1. #1
    Sencha User
    Join Date
    Apr 2013
    Posts
    9
    Vote Rating
    0
    5LiC is on a distinguished road

      0  

    Default Unanswered: I there a way to automatically populate model from a nested json and a store?

    Unanswered: I there a way to automatically populate model from a nested json and a store?


    Hey Guys,


    I am new to Sencha Touch and currently am using ver - 2.1.1


    I have the following models and stores set up. The original models and stores are too big and detailed so using a smaller subset to explain my question.


    Player model is such that it has some fields and can have multiple currencies.
    Code:
    Ext.define('MyApp.model.Player', {
        extend: 'Ext.data.Model',
    
    
        requires: [
            'MyApp.model.Currency',
        ],
    
    
        config: {
            fields: [
                {name: 'id', type: 'int'},
                {name: 'name', type: 'string'},
                {name: 'image_url', type: 'string'},
            ],
            associations: [
                {type: 'hasMany', model: 'MyApp.model.Currency', name: 'balances', associationKey: 'balances', primaryKey:'id', foreign_key:'player_id'}, 
            ],
            proxy: {
                type: 'rest',
                url: '/players'
                format: 'json'
            }
        }
    });
    The Player store is configured to autoload and gives a JSON response as follows
    Code:
    {
      "balances": [
        {
          "amount": 7, 
          "id": 1
        }, 
        {
          "amount": 11, 
          "id": 2
        }
      ], 
      "id": 122, 
      "image_url": "/image.png", 
      "name": "Jason"
    }
    Currency Model is such that
    Code:
    Ext.define('MyApp.model.Currency', {
        extend: 'Ext.data.Model',
    
    
        config: {
            fields: [
                {name: 'id', type: 'int'},
                {name: 'name', type: 'string'},
                {name: 'amount', type: 'string'},
                {name: 'image_url', type: 'string'}
            ],
            associations: [
                {type: 'belongsTo', model: 'MyApp.model.Player'},
                {type: 'belongsTo', model: 'MyApp.model.Bet'}
            ],
            proxy: {
                type: 'rest',
                url: '/currencies',
                format: 'json'
            }
        }
    });
    The Currency store is configured to autoload all the required currencies for displaying in the store (hence the amount field is absent)
    Code:
    [
        {
            "id": 1,
            "name": "Gold",
            "image_url": "gold.png",
        },
        {
            "id": 2,
            "name": "Diamonds",
            "image_url": "diamond.png",
        }
    ]

    So when I get the player from the player store I run into this issue
    Code:
    var playerStore = Ext.getStore('Players');
    var user = playerStore.first();
    user.balances().each(function(balance){
        console.log(balance.get('amount'));    //Works
        console.log(balance.get('id'));    //Works
        console.log(balance.get('name')); //Gives undefined
        console.log(balance.get('image_url')); //Gives undefined
    
    
        //To get these I have to do the following
        var currencyStore = Ext.getStore('Currencies');
        var currency = currencyStore.findRecord('id', balance.get('id'));
        console.log(currency.get('name')); //Works
        console.log(currency.get('image_url')); //Works
    });

    So my question is (finally :P),


    Is there a way to get a model partially from nested json and partially from an available store? Since they share the model and it already has a proxy set up, maybe we can update the missing information and have it be available for direct use?
    This is so that I can limit the server nested JSON response and reuse some of the data thats already available to the app.

    Thanks,

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,399
    Answers
    3549
    Vote Rating
    849
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You would have to manually get the data from the available store and set the fields on the record. The loading will only use the data that is loaded.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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.

  3. #3
    Sencha User
    Join Date
    Apr 2013
    Posts
    9
    Vote Rating
    0
    5LiC is on a distinguished road

      0  

    Default


    As per my understanding the associations will create a new store that will map to the nested json correct?

    Is there a way I can specify the store on this association such that the store data is loaded before the nested json data?

    something like this?
    Code:
    associations: [
        {type: 'belongsTo', model: 'MyApp.model.Currency', associationKey: 'balances',
            store:{model:'MyApp.model.Currency', autoLoad:true,
                proxy:{
                    type: 'rest',
                    url: '/currencies',
                    format: 'json'
                }
            }
        }
    ]
    The above doesn't really work, it just adds a balances store that has all the currency data on it. I still need to manually copy over the data

    But populating from nested json and a previously loaded store seems like a nice to have feature

  4. #4
    Sencha User
    Join Date
    Apr 2013
    Posts
    9
    Vote Rating
    0
    5LiC is on a distinguished road

      0  

    Default


    Update: I recently upgraded to 2.2 and this is the result I am getting.

    Currencies store loads such a JSON
    Code:
    [
        {
            "id": 1,
            "name": "Gold",
            "image_url": "gold.png"
        },
        {
            "id": 2,
            "name": "Diamonds",
            "image_url": "diamond.png"
        }
    ]
    Player store loads such a JSON
    Code:
    {
        "balances": [
            {
                "amount": 7,
                "id": 1
            },
            {
                "amount": 11,
                "id": 2
            }
        ],
        "id": 122,
        "image_url": "/image.png",
        "name": "Jason"
    }

    Now when I get the player record from the player store, the nested json is loaded in data and the currency store data is loaded in raw. So following works:

    Code:
    var playerRecord = Ext.getStore('Players').first();
    playerRecord.balances().each(function (balance) {
    
        //Things that can be accessed directly from the record  
        console.log(balance.get('amount'));
        console.log(balance.get('id'));
        
        //These fail. Show up as null or undefined.
        console.log(balance.get('image_url'));
        console.log(balance.get('name'));
        
        //These work
        console.log(balance.raw.image_url);
        console.log(balance.raw.name);
    });
    Is this a bug? Or is this how it should work? In similar scenarios will the raw data be dependable?

    Ideally what I would like is to access each value through the get method on the record.

    My current work around is to manually copy over the raw fields to the data fields when the store loads.

    More info on this would indeed be helpful.

    Thanks!!
    Last edited by 5LiC; 17 May 2013 at 11:02 AM. Reason: typo in code

Thread Participants: 1

Tags for this Thread