Results 1 to 8 of 8

Thread: Loading Nested Data in Associations

  1. #1
    Sencha User
    Join Date
    Jun 2012
    Posts
    19
    Answers
    1

    Default Answered: Loading Nested Data in Associations

    Hi guys,

    I am trying to add nested data to a store with a model with associations. So, I have:

    User.js
    Code:
    Ext.define('Fiddle.model.User',{    extend: 'Ext.data.Model',
        fields: [{
            name: 'name',
            type: 'string'
        }]
    });
    Product.js
    Code:
    Ext.define('Fiddle.model.Product',{    extend: 'Ext.data.Model',
        fields: [{
            name: 'name',
            type: 'string'
        },{
            name: 'user_id',
            reference: 'User'
        }]
    });
    and a store referencing the User model
    Code:
    Ext.define('Fiddle.store.Users', {    extend: 'Ext.data.Store',
        config: {
            autoLoad: true,
            model: 'Fiddle.model.User'
        }
    });
    When I add nested data and try to obtain the information, I get an error:
    Code:
    var users = Ext.create('Fiddle.store.Users');users.add({
        name: 'User1',
        products:[{
            name: 'product1'
        },{
            name: 'product2'
        }]
    });
    users.getAt(0).products().getAt(0).get('name');
    I note that the 'products' property is assigned directly to the model instance and it is not possible to get the store of products.

    Can anyone help me with this? I appreciate any suggestion.

    Here is the fiddle link: https://fiddle.sencha.com/fiddle/vti

    Thanks

  2. Hi--

    I'd add a base model with the schema for your models (e.g., 'Fiddle.model'). That will let you use the current syntax that you have in the association.

    Next, you should use loadRawData() to add the data to the store so that the reader will be engaged and process the association data.

    http://docs.sencha.com/extjs/6.0/6.0...od-loadRawData

    Here's an updated example:

    https://fiddle.sencha.com/#fiddle/vtq

    Thanks!
    Joel

  3. #2
    Sencha User joel.watson's Avatar
    Join Date
    Nov 2014
    Posts
    3,120
    Answers
    446

    Default

    Hi--

    I'd add a base model with the schema for your models (e.g., 'Fiddle.model'). That will let you use the current syntax that you have in the association.

    Next, you should use loadRawData() to add the data to the store so that the reader will be engaged and process the association data.

    http://docs.sencha.com/extjs/6.0/6.0...od-loadRawData

    Here's an updated example:

    https://fiddle.sencha.com/#fiddle/vtq

    Thanks!
    Joel

  4. #3
    Sencha User
    Join Date
    Jun 2012
    Posts
    19
    Answers
    1

    Default Hi

    Hi Joel,

    Thanks for your answer. It works!

    I have some other questions about this topic, for example, it is possible customize the property's name for access the association's store? With the example above, it is possible change the "products" accesor?

    In the same way, if the data hava other property name, for example "userProducts",how can I configure the store or the model for mapping that?

    I know that in touch it was possible through the hasMany and belongsTo properties. With the scheme I cant figure out how to do this.

    Thanks again.

  5. #4
    Sencha User joel.watson's Avatar
    Join Date
    Nov 2014
    Posts
    3,120
    Answers
    446

    Default

    Quote Originally Posted by jserna View Post
    Hi Joel,

    Thanks for your answer. It works!

    I have some other questions about this topic, for example, it is possible customize the property's name for access the association's store? With the example above, it is possible change the "products" accesor?

    In the same way, if the data hava other property name, for example "userProducts",how can I configure the store or the model for mapping that?

    I know that in touch it was possible through the hasMany and belongsTo properties. With the scheme I cant figure out how to do this.

    Thanks again.
    You can do this via the inverse configuration for the reference:

    Code:
    inverse: {
        role: 'userProducts'
    }
    I've updated the Fiddle with an example: https://fiddle.sencha.com/#fiddle/vtq

    Thanks
    Joel

  6. #5
    Sencha User
    Join Date
    Jun 2012
    Posts
    19
    Answers
    1

    Default Thanks Joel!

    Joel, thanks so much for your time.

  7. #6
    Sencha User joel.watson's Avatar
    Join Date
    Nov 2014
    Posts
    3,120
    Answers
    446

    Default

    Quote Originally Posted by jserna View Post
    Joel, thanks so much for your time.
    Sure thing, happy to help!
    Thanks
    Joel

  8. #7
    Sencha Premium User alexander.urban's Avatar
    Join Date
    Nov 2013
    Posts
    320
    Answers
    9

    Default

    Hi Joel,

    sorry for taking this thread up from the dead again, but I could use a little help with the Fiddle sample.

    My JSON properties are uppercase. If you replace all occurrences of "userProducts" with "UserProducts" in your last fiddle, console.log will output 0, not 2. Why is that and how to fix?

    Kind regards,

    Alexander

  9. #8
    Sencha User
    Join Date
    Jul 2017
    Posts
    1

    Default

    Quote Originally Posted by alexander.urban View Post
    Hi Joel,

    sorry for taking this thread up from the dead again, but I could use a little help with the Fiddle sample.

    My JSON properties are uppercase. If you replace all occurrences of "userProducts" with "UserProducts" in your last fiddle, console.log will output 0, not 2. Why is that and how to fix?

    Kind regards,

    Alexander
    +1
    We have same issues and would like to know a possible solution.

    Regards,
    Eugene

Similar Threads

  1. Replies: 4
    Last Post: 7 Jan 2015, 9:35 AM
  2. Replies: 2
    Last Post: 6 Jul 2012, 5:59 AM
  3. Nested json data, models and associations
    By RunPixelRun in forum Sencha Touch 1.x: Q&A
    Replies: 2
    Last Post: 7 Feb 2012, 3:04 PM
  4. Saving nested data with associations
    By DavidSSL in forum Ext: Discussion
    Replies: 6
    Last Post: 24 Aug 2011, 10:47 PM

Posting Permissions

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