Results 1 to 9 of 9

Thread: ST2 Store & JSON Reader: How to retrieve custom metadata fields?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Sencha User
    Join Date
    Jun 2009
    Posts
    47
    Answers
    3
    Vote Rating
    0
      0  

    Default 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.


    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"
                }
            ]
        }
    Please let me know if I can provide any clarifications. Any help is greatly appreciated. Thanks in advance.


    Mohammad
    San Jose, CA

  2. Lets say we have this json:

    Code:
    {
        "data" : [
            {
                "test" : "hi"
            }
        ],
        "foo"  : "Blam!"
    }
    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:
    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);
            })();
        }
    });
    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:
    new MyApp.store.MyStore({
        autoLoad  : true,
        listeners : {
            load : function (store) {
                var foo = store.getFoo();
    
                console.log(foo); //Blam!
            }
        }
    });

  3. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,416
    Answers
    3899
    Vote Rating
    1270
      0  

    Default

    Lets say we have this json:

    Code:
    {
        "data" : [
            {
                "test" : "hi"
            }
        ],
        "foo"  : "Blam!"
    }
    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:
    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);
            })();
        }
    });
    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:
    new MyApp.store.MyStore({
        autoLoad  : true,
        listeners : {
            load : function (store) {
                var foo = store.getFoo();
    
                console.log(foo); //Blam!
            }
        }
    });
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it!

    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
    Join Date
    Jun 2009
    Posts
    47
    Answers
    3
    Vote Rating
    0
      0  

    Default 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. #4
    Sencha User
    Join Date
    Dec 2008
    Posts
    9
    Vote Rating
    0
      0  

    Default

    use this 1-line code to get your query_time value:
    Code:
    console.log(YourDataStore.reader.jsonData.query_time);

  6. #5
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,416
    Answers
    3899
    Vote Rating
    1270
      0  

    Default

    Quote Originally Posted by shab View Post
    use this 1-line code to get your query_time value:
    Code:
    console.log(YourDataStore.reader.jsonData.query_time);
    smaller code !== best code
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it!

    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.

  7. #6
    Touch Premium Member
    Join Date
    Apr 2011
    Location
    Southern CA, Orange County
    Posts
    11
    Vote Rating
    3
      1  

    Default ExtJs5 solution?

    I assume this solution was demonstrated for ExtJs 4.x. Did ExtJs 5.x make any changes to make metadata more accessible and available? Or is this still the best code to use in ExtJs 5?

  8. #7
    Sencha Premium Member
    Join Date
    Jul 2015
    Posts
    33
    Answers
    1
    Vote Rating
    6
      0  

    Default

    Hi Mitchel,

    This works perfectly and much better then the hacks I was thinking of making.

    One question to ditto Ken. Is this still the best way to achieve this using Ext 5/6? I see that in 6 keepRawData is by default set to false to avoid memory leaks. I've set it to true to get this to work but I'm just wondering if there is a better approach now?

    Thanks

    Herbie

  9. #8
    Sencha User
    Join Date
    Sep 2015
    Posts
    21
    Vote Rating
    1
      0  

    Default

    Quote Originally Posted by HerbiePorter View Post
    Hi Mitchel,

    This works perfectly and much better then the hacks I was thinking of making.

    One question to ditto Ken. Is this still the best way to achieve this using Ext 5/6? I see that in 6 keepRawData is by default set to false to avoid memory leaks. I've set it to true to get this to work but I'm just wondering if there is a better approach now?

    Thanks

    Herbie
    Good question. I'd like to hear Sencha's opinion on this too.

  10. #9
    Sencha User
    Join Date
    Jun 2014
    Location
    Amsterdam
    Posts
    3
    Vote Rating
    0
      0  

    Default

    HelloThis post says you can't set a custom property with a value of null in Architect: http://www.sencha.com/forum/showthre...null-valueDoes that mean this solution can't be used in Architect?AndrewAmsterdam

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •