Hi,
as example I have an user store which should load/update its content based on user login/logout events. Currently I setup the event listening in the overriden store constructor like this:
Code:
Ext.define('MyApp.store.app.User', {
extend: 'Ext.data.Store',
model: 'MyApp.model.app.User',
autoLoad: true,
autoSync: true,
// We override the constructor to add a listener for login events to this store
constructor: function () {
this.isContructing = true;
Ext.data.Store.prototype.constructor.apply(this, arguments);
MyApp.comm.app.on({
login: function() {
// Reload the store when user logs in but sync any unsaved items before
this.sync();
this.load();
},
beforelogout: function() {
// Try to sync all changes before we're flushing the store on user logout
this.sync();
this.removeAll();
},
scope: this
});
this.isContructing = false;
}
});
Now I'm not sure if there exists any better approach instead of overwriting the store constructor?
MyApp.comm.app is my "global application event bus" where login & logout events are fired. Listening in controllers for these events might lead to several view components (configured with this store) receiving those events and therefore triggering store reloading a multiple times. Therefore I moved the event listening to the store itself.
Thanks for any comments or suggestions!