1. #1
    Sencha User
    Join Date
    Apr 2013
    Posts
    3
    Vote Rating
    1
    jorisschutijzer is on a distinguished road

      0  

    Default Answered: hasMany association no accessor

    Answered: hasMany association no accessor


    According to the documentation if i define a hasMany association, i get an accessor for this association

    hasMany: {
    model: 'Product',
    name: 'products'
    },

    parentModel.products()

    1. I'm not getting this accessor. Does anyone know why?

    2. Apparently this accessor is only there if you autoloaded the parent. But what to do if you start with a new instance of the model and there is nothing to be loaded yet?

    3. How do I add model object to the association?

  2. The accessor should exist right after the model instance is created:

    Code:
    Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: ['id', 'name'],
        proxy: {
            type: 'rest',
            url : 'data/users',
            reader: {
                type: 'json',
                root: 'users'
            }
        },
    
    
        hasMany: 'Post' // shorthand for { model: 'Post', name: 'posts' }
    });
    
    
    Ext.define('Post', {
        extend: 'Ext.data.Model',
        fields: ['id', 'user_id', 'title', 'body'],
    
    
        proxy: {
            type: 'rest',
            url : 'data/posts',
            reader: {
                type: 'json',
                root: 'posts'
            }
        },
        belongsTo: 'User'
    });
    
    
    var user = Ext.create('User');
    console.log(user);
    You can create a new instance of the parent model and its associated store will exist with no models (until it's loaded). You can load nested data directly to the model hierarchy or you can load associated data only as needed.
    See the associations sub-section of the Data guide:
    http://docs.sencha.com/extjs/4.2.0/#!/guide/data

    You might also check out this blog post talking about loading associated data locally - versus from a remote URL:
    http://mitchellsimoens.com/2013/03/e...-associations/

  3. #2
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    5,468
    Answers
    441
    Vote Rating
    202
    slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of slemmon has much to be proud of

      0  

    Default


    The accessor should exist right after the model instance is created:

    Code:
    Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: ['id', 'name'],
        proxy: {
            type: 'rest',
            url : 'data/users',
            reader: {
                type: 'json',
                root: 'users'
            }
        },
    
    
        hasMany: 'Post' // shorthand for { model: 'Post', name: 'posts' }
    });
    
    
    Ext.define('Post', {
        extend: 'Ext.data.Model',
        fields: ['id', 'user_id', 'title', 'body'],
    
    
        proxy: {
            type: 'rest',
            url : 'data/posts',
            reader: {
                type: 'json',
                root: 'posts'
            }
        },
        belongsTo: 'User'
    });
    
    
    var user = Ext.create('User');
    console.log(user);
    You can create a new instance of the parent model and its associated store will exist with no models (until it's loaded). You can load nested data directly to the model hierarchy or you can load associated data only as needed.
    See the associations sub-section of the Data guide:
    http://docs.sencha.com/extjs/4.2.0/#!/guide/data

    You might also check out this blog post talking about loading associated data locally - versus from a remote URL:
    http://mitchellsimoens.com/2013/03/e...-associations/

  4. #3
    Sencha User
    Join Date
    Apr 2013
    Posts
    3
    Vote Rating
    1
    jorisschutijzer is on a distinguished road

      1  

    Default


    After a lot of searching, I found out that I was missing a required.

Thread Participants: 1