View Full Version : Modify models before are created in the store
24 Aug 2012, 5:11 PM
I'm creating a small application which it makes a request to an external API and populates a store (by a proxy). AFAIK, the store creates a collection of models.
What I'm trying to do is to modify received data before the model it's being created. For example:
I have a model with fields: id, title, description
I need to override some method invoked by store to parse title json data and save it parsed. The question is, what method should I override or what event should I bind for it?
Thanks in advance!
24 Aug 2012, 7:50 PM
You should try doing an Ajax or JsonP request to retrieve the data then. Then you will have the raw data in the response and you can build your models manually that way.
25 Aug 2012, 1:59 PM
I found a possible solution, but I'm not sure if it could have some performance issues.
I created the store with a proxy requesting for the data. The autoload attribute was set to false.
Then, in the controller, I defined the launch method which it gets the store, set an on-load listener and finally, call to mystore.load() method.
The onLoad handler, loops over the models structure and for each item on it, first, modify the field and then it call to item.set(field, value).
This did the work, but what I am wondering if the field is being set two times (first time when the store load the original data, and other, when I set the new parsed value). If it was this case, it could have some issues of performance because we are looping over an n-sized structure, doing two times, will have double effort.
26 Aug 2012, 5:49 PM
Maybe you can do this with a small change to your Model definition. If you set a convert function on the title data field (Model definition), you can do whatever you want to the raw data before it gets added to the Record.
Check out the Types and conversions section of the documentation (near the top): http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Field
Also make sure to read the convert property docs: http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Field-cfg-convert
28 Aug 2012, 4:28 PM
Hey dude, Thanks! This makes what I was searching ;)
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.