-
8 Oct 2012 1:39 AM #1
Answered: Build a view from json data & have 2 proxies in one store
Answered: Build a view from json data & have 2 proxies in one store
Hello,
First question
I would like to build a view from data comming from JSON.
I intended to store the json data into a model (and cache it) and then build the view from the model.
Is it possible to do?
Second question
I have the following json:
How could I retrieve the data field and the ui field and store each of them in a separate model?Code:{ "data":[{"name":"Alice","age":"25"},{"name":"Bob","age":"35"},{"name":"Cedric","age":"45"},{"name":"Delphine","age":"55"},{"name":"Eleonore","age":"65"}], "ui":{"button":123,"age":1234} }
I tried to do it with 2 proxies with different rootProperty, but only the latest one is considered.
Thank you for your support,Code:proxy: { type: 'ajax', //url : 'http://localhost:8888/patientData.json', url : 'http://localhost:8888/test.php', // with data & ui reader: { type: 'json', model: 'PhoneGap.model.patientUI', rootProperty: 'ui' } }, proxy: { type: 'ajax', url : 'http://localhost:8888/test.php', // with data & ui reader: { type: 'json', model: 'PhoneGap.model.patientModel', rootProperty: 'data' } },
-
Best Answer Posted by mitchellsimoens
First question: Yes you can.
Second question: of course that is not going to work, you can't have an object with duplicate properties.
Here is how you can get the ui property:
And the instance:Code:Ext.define('ModelA', { extend : 'Ext.data.Model', config : { fields : [ 'name', 'age' ] } }); Ext.define('ModelB', { extend : 'Ext.data.Model', config : { fields : [ 'button', 'age' ] } }); Ext.define('Store', { extend : 'Ext.data.Store', config : { uiProperty : 'ui', model : 'ModelA', proxy : { type : 'ajax', url : 'data/json.json', reader : { rootProperty : 'data' } } }, getUI : function() { var reader = this.getProxy().getReader(), rawData = reader.rawData, fn = reader.createAccessor(this.getUiProperty()); return fn(rawData); } });
Code:new Store({ autoLoad : true, listeners : { load : function(store, recs) { var ui = store.getUI(); console.log(ui); } } });
-
10 Oct 2012 4:58 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
First question: Yes you can.
Second question: of course that is not going to work, you can't have an object with duplicate properties.
Here is how you can get the ui property:
And the instance:Code:Ext.define('ModelA', { extend : 'Ext.data.Model', config : { fields : [ 'name', 'age' ] } }); Ext.define('ModelB', { extend : 'Ext.data.Model', config : { fields : [ 'button', 'age' ] } }); Ext.define('Store', { extend : 'Ext.data.Store', config : { uiProperty : 'ui', model : 'ModelA', proxy : { type : 'ajax', url : 'data/json.json', reader : { rootProperty : 'data' } } }, getUI : function() { var reader = this.getProxy().getReader(), rawData = reader.rawData, fn = reader.createAccessor(this.getUiProperty()); return fn(rawData); } });
Code:new Store({ autoLoad : true, listeners : { load : function(store, recs) { var ui = store.getUI(); console.log(ui); } } });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.
-
10 Oct 2012 5:42 AM #3
Thank you for your answer !!
However, in your example you only retrieve the model A from the JSON.
How to retrieve model A & B from JSON data and store them? Do I need to use 2 stores?
Or should I use 1 main model that has 2 submodel (ui and data)?
-
10 Oct 2012 7:07 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 438
- Answers
- 3113
My example you get the object from the ui root, therefore you can create a new model with that object, gotta leave something for you to do
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.
-
10 Oct 2012 7:18 AM #5
Thank you.
I managed to do it meanwhile


Reply With Quote