John Krull
John Krull

Reputation: 302

How can I make an Ext JS 4 model association use an existing store rather than creating its own?

I have the following models set up (simplified):

Ext.define('UserGroup', {
    extend: 'Ext.data.Model',
    fields: ['GroupID', 'UserID'],
    idProperty: '',
    associations: [{
        type: 'belongsTo',
        model: 'User',
        primaryKey: 'UserID',
        foreignKey: 'UserID',
        name: 'users'
    }]
)};

Ext.define('User', {
    extend: 'Ext.data.Model',
    idProperty: 'UserID',
    associations: [{
        type: 'hasMany',
        model: 'UserGroup',
        primaryKey: 'UserID',
        foreignKey: 'UserID',
        name: 'userGroups'
    }]
});

I already have existing stores loaded for each of these models, with plenty of associated data. When I try to access the UserGroup association from User, I get no data back.

Ext.getStore('User').getAt(12).userGroups().count(); // returns 0

I know this is because assocation is creating its own store. So far, I can only get the appropriately associated data by

Ext.getStore('User').getAt(12).userGroups().load();

which makes a call to the server to get that information, but then I have to use a callback and wait for a response.

So, how can I specify a store for the userGroups assocation to use so it doesn't request data from the server?

Upvotes: 2

Views: 1423

Answers (1)

Molecular Man
Molecular Man

Reputation: 22386

Afaik, there are two possible solutions:

  1. Override Ext.data.HasManyAssociation.createStore.
  2. Pass the data from original store to Ext.data.HasManyAssociation.storeConfig:

    Ext.define('User', {
        extend: 'Ext.data.Model',
        idProperty: 'UserID',
        associations: [{
            type: 'hasMany',
            storeConfig: {
              data: originalStore.getRange()
            },
            // ...
        }]
    });
    

Upvotes: 3

Related Questions