PDA

View Full Version : HasMany Association Returns Empty Store



Sneeper
17 May 2012, 9:39 AM
Hi there,

I scoured the forums and found similar issues to mine with people using memory proxies, however my problem is that I have two models defined; one of which has a HasMany association with the other:

Model A


Ext.define('nmiReporting.model.record_nmi', {
extend: 'Ext.data.Model',
alias: 'model.record_nmi',
uses: [
'nmiReporting.model.nmiEvents'
],

idProperty: 'id',

fields: [ ... ],

hasMany: {
model: 'nmiReporting.model.nmiEvents',
name: 'events'
}
});


Model B


Ext.define('nmiReporting.model.nmiEvents', {
extend: 'Ext.data.Model',
alias: 'model.nmiEvents',

idProperty: 'event_id',

fields: [ ... ],

proxy: {
type: 'ajax',
api: {
create: '../resources/_php/api.php?create',
read: '../resources/_php/api.php?read',
update: '../resources/_php/api.php?update',
destroy: '../resources/_php/api.php?destroy'
},
extraParams: {
table: 'record_events',
uid: window.sessionStorage.personId
},
noCache: false,
url: 'http://cesis.mtsallstream.com/resources/_php/api.php',
reader: {
type: 'json',
idProperty: 'event_id',
messageProperty: 'msg',
root: 'data'
}
}
});


I can instantiate the store just as the documentation says:



var rec = Ext.getCmp('nmiForm').getForm().getRecord();
var events = rec.events().load();
/* at this point the events store is empty */
events.on('load', function(store, records, success, options){

/* store is still empty, but records is populated. So the only way I can get the records in to
* the store is by manually adding them :( */
store.add(records);

/* reconfigure the grid */
Ext.getCmp('gridEvents').reconfigure(store);
});


But as you can read by the code above, the load event fired but the store is empty. But the records argument is full. My question is; why doesn't the store populate with the load() call?

Regards,

Rob

Sneeper
23 May 2012, 9:45 AM
anyone?

ivanhu
23 May 2012, 10:34 AM
Your main model should know the id field of the event. Try to add the

associationKey: 'event_id'

property to the hasMany. Also, in my submodels I use the

belongsTo: 'main_model'

property too. With REST, it works; never did try with Ajax though.

Sneeper
23 May 2012, 12:24 PM
Thanks for the help, your post made me rethink my model and I have to admit I overlooked something very simple. Instead of using the associationKey, I used the forigenKey because I realised that the record_nmi is related to record_events by the nmir_id field, not the id field:



hasMany: {
model: 'nmiReporting.model.nmiEvents',
foreignKey: 'nmir_id',
name: 'events'
}


the model now works as expected :) Thanks again for the push!