-
2 Feb 2013 8:39 AM #1
Answered: ST2 Store & JSON Reader: How to retrieve custom metadata fields?
Answered: ST2 Store & JSON Reader: How to retrieve custom metadata fields?
Hello,
I'm a newbie to ST, so this may sound like a basic question.
I have tried to look for a solution for this but have not been able to find one. Basically, I have a store and proxy setup to load JSON data. I wanted to see if there was a way to get value for fields that are outside the actual results array. I know we can set some properties like "totalProperty" and "messageProperty" and access these with ResultSet methods like "getTotal()" and "getMessage()". How about custom fields? Anyway to access those?
Below is an example of what I'm talking about. How can I setup my store/proxy/reader setup to be able to retrieve the values for "query_id" and "query_time"? I do not need to store these in a store or anything, I just need to capture these and store them in variables. I can use "totalProperty" and "messageProperty" to get the query_total and query_message.
Please let me know if I can provide any clarifications. Any help is greatly appreciated. Thanks in advance.Code:{ "success": true, "query_id": "ID-1234", "query_time": "19837382849", "query_total": "227", "query_message": "Successfully retrieved", "users": [ { "firstName": "Tommy", "lastName": "Maintz", "age": 24, "eyeColor": "green" }, { "firstName": "Aaron", "lastName": "Conran", "age": 26, "eyeColor": "blue" }, { "firstName": "Jamie", "lastName": "Avins", "age": 37, "eyeColor": "brown" } ] }
Mohammad
San Jose, CA
-
Best Answer Posted by mitchellsimoens
Lets say we have this json:
The data array is going to be the data for the store as we will set rootProperty : 'data' but maybe we want to get the value for foo. We need to use createAccessor for this:Code:{ "data" : [ { "test" : "hi" } ], "foo" : "Blam!" }
The getFoo and getFooFn are the important pieces. The getFooFn is the private property that holds the actual accessor method, this method is exactly like getTotal and getMessage only it will return the value for foo when passed in the raw data. So now when you execute getFoo it will return 'Blam!'. Here is a test:Code:Ext.define('MyApp.store.MyStore', { extend : 'Ext.data.Store', config : { fields : [ 'test' ], proxy : { type : 'ajax', url : 'data/json.json', reader : { type : 'json', rootProperty : 'data' } } }, getFooFn : null, getFoo : function () { var me = this, proxy = me.getProxy(), reader = proxy.getReader(), getFoo = me.getFooFn, rawData; if (!getFoo) { getFoo = me.getFooFn = reader.createAccessor('foo'); } return (function () { rawData = reader.rawData; return getFoo(rawData); })(); } });
Code:new MyApp.store.MyStore({ autoLoad : true, listeners : { load : function (store) { var foo = store.getFoo(); console.log(foo); //Blam! } } });
-
4 Feb 2013 6:28 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,107
- Vote Rating
- 453
- Answers
- 3157
Lets say we have this json:
The data array is going to be the data for the store as we will set rootProperty : 'data' but maybe we want to get the value for foo. We need to use createAccessor for this:Code:{ "data" : [ { "test" : "hi" } ], "foo" : "Blam!" }
The getFoo and getFooFn are the important pieces. The getFooFn is the private property that holds the actual accessor method, this method is exactly like getTotal and getMessage only it will return the value for foo when passed in the raw data. So now when you execute getFoo it will return 'Blam!'. Here is a test:Code:Ext.define('MyApp.store.MyStore', { extend : 'Ext.data.Store', config : { fields : [ 'test' ], proxy : { type : 'ajax', url : 'data/json.json', reader : { type : 'json', rootProperty : 'data' } } }, getFooFn : null, getFoo : function () { var me = this, proxy = me.getProxy(), reader = proxy.getReader(), getFoo = me.getFooFn, rawData; if (!getFoo) { getFoo = me.getFooFn = reader.createAccessor('foo'); } return (function () { rawData = reader.rawData; return getFoo(rawData); })(); } });
Code:new MyApp.store.MyStore({ autoLoad : true, listeners : { load : function (store) { var foo = store.getFoo(); console.log(foo); //Blam! } } });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.
-
4 Feb 2013 8:19 AM #3
Works like a charm
Works like a charm
Thanks a lot! Works just fine. I have not seen examples of this aspect of handling JSON data elsewhere.
Thanks.
-
5 Apr 2013 1:42 PM #4
use this 1-line code to get your query_time value:
Code:console.log(YourDataStore.reader.jsonData.query_time);
-
5 Apr 2013 1:59 PM #5Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 34,107
- Vote Rating
- 453
- Answers
- 3157
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.


Reply With Quote