PDA

View Full Version : Store should listen to login/logout events (Best practice?)



DiscoBoy
8 Feb 2012, 6:22 AM
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:



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!

mitchellsimoens
8 Feb 2012, 8:17 AM
Using MVC this would be easy to fire a login and logout event on the application instance and let the controllers listen for it so your application logic would be in the controller

DiscoBoy
8 Feb 2012, 9:28 AM
I know. But if i have for instance two grids configured with my user store and make them listening to logins, than both grids trigger a store reload (when only one reload is needed). or?

mitchellsimoens
8 Feb 2012, 9:31 AM
Yeah, I wouldn't mess with the grid in the application event listener, would just do what I need on the store and since there is one store, any widgets attached to it will update.

DiscoBoy
8 Feb 2012, 12:20 PM
Thx for your comments. I think then this is the way I continue...