PDA

View Full Version : What is the best way for a Store to get a reference to the hosting MVC application



Curtis Fletcher
3 Jul 2012, 7:55 AM
I have a large Ext 4.1 MVC application that is essentially a cardview, navigation and some side panels, each card may contain grids/forms/etc and these back onto stores that are reused in a number of places/cards.

There are UI elements that can modify "top level" state, that should be represented across all the components that are interested (These would need to be added to the stores' proxies' extraParams the next time they [re]load)

The way I'm currently doing this is to have "XXXchanged" events fired on the application object that interested components could watch for, this is easy to do from a controller but hard to do from a store. The problem is that some of my stores are used across views that result in multiple controllers so there is no "definitive" controller that can speak for a specific store, hence I'd like the Store to register it's interest in these "top level" events directly.

However I don't see any easy way for a store to get a handle on the application it was created for without hardcoding the application's global name or parsing the model's own namespace and walking back until I find the application object and neither of those feel "clean"

What I was hoping for would be an injected-on-instantiation "getApplication" method so each object could get to it's application instance, but I can't see anything similar.

Am I going about this the wrong way? Do you only ever refer to the application by it's global namespace or from controllers?

el_chief
3 Jul 2012, 10:21 AM
In MVC, the model (same for store, which is a collection of models) should never have a reference to controllers, or views.

If you have a model that represents your application, that would be ok

Either way, the best way is to pass a parameter into the store constructor (config). This is called dependency injection.

fschaeffer
3 Jul 2012, 9:02 PM
At least for every component there is the application object available via



this.application.fireEvent('yourEventName', ....);


Maybe this is also available in a store, you could give it a try ....