1. #1
    Sencha User Ola GP's Avatar
    Join Date
    Jan 2013
    Location
    Sweden
    Posts
    28
    Answers
    1
    Vote Rating
    2
    Ola GP is on a distinguished road

      0  

    Default Answered: Best way to change and update store items

    Answered: Best way to change and update store items


    Imagine this scenario:
    • I have a store and model setup with a lot of items of places with their locations recorded (Latitude & Longitude).
    • In the application I have a list view. In this list view the items are listed with their name.
    • By using the Geolocation Api, I can get the users current location.
    • I also have a code snippet that can calculate the distance between two geolocations.
    What I want:
    • In the list, next to each items name, I would like to show the current distance between the users current location and the list items geographical location.
    What I tried:
    • I tried to solve this by listening to the geolocation api update function.
    • Every time the user gets a new location, this event fires. I then loop through the records in the store. Getting each items location and use my custom code snippet that calculates the distance.
    • I then save this calculated distance in a specific field in the store. And by adding this field in the template for the list, I am able to show the distance next to the items name in the list.
    So far so good. But I have encountered some problems and I wonder if there are a better way to solve this.

    The problem #1:
    • The user is able to filter the list view.
    • This means that each time the geolocation update fires and loop through the items in the store, only the filtered items in the store are modified.
    • I don't know how to get access to all the items in a filtered store and to change their values.
    The problem #2:
    • Even if I find a way to alter all the items in the store, I'm afraid that this would not be very efficient. If there are a lot of items, the loop and data processing might take a long time.
    • Since the list view always is filtered in some way, not all store items is shown on screen at the same time.
    Possible solution?
    • I have an idea to solve this but I don't know if it's possible.
    • Can I in some way modify the store's model to add a field that acts as a function that calculates the distance on-the-fly?
    • This way the distance is only calculated when the distance field is used in an items template.
    • Something like this:
      (Note: just example code, I don't know the syntax.)
    Code:
    Ext.define('App.model.Places', {
        extend: 'Ext.data.Model',
    
    
        config: {
            proxy: {
                type: 'jsonp',
                url : 'http://example.com/ajax/places.php',
                reader: {
                    type: 'json',
                    rootProperty: 'places'
                }
            },
            
            fields: [
    'Name', 
    'Address',
     'Latitude',
    'Longitude',
    'Distance' : function() { calculateDistance(this.Latitude, this.Longotude); }
    ]
        }
    });
    What do you think? What would be the best way to solve this problem?

  2. You can have a field and use the convert config, here is a sample of concating first/last names:

    Code:
    fields : [
        'firstName',
        'lastName',
        {
            name    : 'fullName',
            convert : function(val, rec) {
                return rec.get('lastName') + ', ' + rec.get('firstName');
            }
        }
    ]
    For the convert method to be executed, it must be set to a value. So if you want to change the firstName value, the fullName will NOT automatically update, you can do this via:

    Code:
    rec.set({
        firstName : 'foo',
        fullName : ''
    });
    No matter what you set it to, it will get overridden by the convert function but you have to set it to something.

    I think you can use that to your liking but here's what I'd do...

    When you get the user's location, kick off a method that will iterate through the store's active data set (not all the records) as you are doing, it's fine that this won't touch the records that are filtered out of the active data set. You can then listen to the store's refresh event and execute that method that will iterate through the store's active data set. The refresh event will fire for a couple things, when a store loads, when it is sorted or filtered.

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    35,735
    Answers
    3364
    Vote Rating
    756
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You can have a field and use the convert config, here is a sample of concating first/last names:

    Code:
    fields : [
        'firstName',
        'lastName',
        {
            name    : 'fullName',
            convert : function(val, rec) {
                return rec.get('lastName') + ', ' + rec.get('firstName');
            }
        }
    ]
    For the convert method to be executed, it must be set to a value. So if you want to change the firstName value, the fullName will NOT automatically update, you can do this via:

    Code:
    rec.set({
        firstName : 'foo',
        fullName : ''
    });
    No matter what you set it to, it will get overridden by the convert function but you have to set it to something.

    I think you can use that to your liking but here's what I'd do...

    When you get the user's location, kick off a method that will iterate through the store's active data set (not all the records) as you are doing, it's fine that this won't touch the records that are filtered out of the active data set. You can then listen to the store's refresh event and execute that method that will iterate through the store's active data set. The refresh event will fire for a couple things, when a store loads, when it is sorted or filtered.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User Ola GP's Avatar
    Join Date
    Jan 2013
    Location
    Sweden
    Posts
    28
    Answers
    1
    Vote Rating
    2
    Ola GP is on a distinguished road

      0  

    Default


    Ok cool, thanks for the suggestions! Will try them out.

Thread Participants: 1

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar