-
19 Jan 2013 5:20 AM #1
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.
- 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.
- 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.
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.
- 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.
- 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.)
What do you think? What would be the best way to solve this problem?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); } ] } });
-
Best Answer Posted by mitchellsimoens
You can have a field and use the convert config, here is a sample of concating first/last names:
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:fields : [ 'firstName', 'lastName', { name : 'fullName', convert : function(val, rec) { return rec.get('lastName') + ', ' + rec.get('firstName'); } } ]
No matter what you set it to, it will get overridden by the convert function but you have to set it to something.Code:rec.set({ firstName : 'foo', fullName : '' });
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.
-
21 Jan 2013 10:51 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
You can have a field and use the convert config, here is a sample of concating first/last names:
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:fields : [ 'firstName', 'lastName', { name : 'fullName', convert : function(val, rec) { return rec.get('lastName') + ', ' + rec.get('firstName'); } } ]
No matter what you set it to, it will get overridden by the convert function but you have to set it to something.Code:rec.set({ firstName : 'foo', fullName : '' });
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
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
21 Jan 2013 11:51 PM #3
Ok cool, thanks for the suggestions! Will try them out.


Reply With Quote