PDA

View Full Version : Extended Model - where to put 'on load' code



dr-dan
21 Sep 2012, 8:29 AM
I have a Model defined and I wish to run one of its methods after a record has been loaded/created.

Given that a proxy can be defined on the store or the model, i was surprised to see that only the store is supporting a load event.

So, at the moment I have something like this which feels ugly as I don't want the store making these sorts of assumptions about its records:



Ext.define('Pegfect.store.PegModels', {
extend: 'Ext.data.Store',
model: 'Pegfect.model.PegModel',
autoLoad: false,
autoSync: false,
on: {
load: this.onLoad,
scope: this
},


onLoad: function () {
this.each(function (pegModel) {
pegModel.setupQuestionMonitors();
});
}


});



Is there another approach?

metalinspired
23 Sep 2012, 1:11 AM
I don't see anything wrong with this approach.
You might want to use add() (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Store-event-add) event instead of load() because if you programmatically add an record to a store load() will not fire.

dr-dan
23 Sep 2012, 8:07 AM
Ok thanks for the headsup on add/load good to know.

I'll guess I'll go with it then, shame there isn't some sort of record.afterSetValues event or something...

metalinspired
23 Sep 2012, 9:10 AM
For something like record.afterSetValues() check the datachanged() (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.AbstractStore-event-datachanged) event.