-
8 Feb 2012 6:22 AM #1
Unanswered: Store should listen to login/logout events (Best practice?)
Unanswered: Store should listen to login/logout events (Best practice?)
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:
Now I'm not sure if there exists any better approach instead of overwriting the store constructor?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; } });
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!
-
8 Feb 2012 8:17 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 436
- Answers
- 3113
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
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
8 Feb 2012 9:28 AM #3
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?
-
8 Feb 2012 9:31 AM #4Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,714
- Vote Rating
- 436
- Answers
- 3113
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.
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
8 Feb 2012 12:20 PM #5
Thx for your comments. I think then this is the way I continue...


Reply With Quote