PDA

View Full Version : MVC: How to access this.application from view?



Spenna
4 Jan 2012, 6:53 AM
Hello everyone.
I have a composite component defined as a view that i 'share' across forms in my app. It contains two combos, and i want to pre-load the items in the first combo from within the view itself. However the store for the combo must be filtered based on a property that i set dynamically in the application - so i want do do something like this:


// in view file
Ext.getStore('Warehouses').filters.clear();
Ext.getStore('Warehouses').filter([
{property:'company_id', value: this.application.getActiveUser().get('company_id')},
{property:'type', value: 'warehouse'}
]);

Ext.getStore('Warehouses').load(); // preload combo


Anyone have a clue how to achieve this?

mitchellsimoens
4 Jan 2012, 7:50 AM
The application instance isn't saved anywhere. I manually do this in the launch method...



Ext.application({
name : 'MyApp',

launch : function() {
MyApp.app = this;
}
});

Now you should be able to resolve the application instance from anywhere using MyApp.app

slemmon
4 Jan 2012, 1:42 PM
Very nice. I was wondering how to do that myself.

Spenna
5 Jan 2012, 12:03 AM
Works, thanks alot!
I have another design-related question regarding this - when re-using a store, i.e 'Warehouses' in my example above, where I want to specify a filter for the store for each component it is bound to - is there a better way of doing it than above? In some other context i use another company_id than the active users and it should also be filtered on another type i.e:



this.getStore('Warehouses').filters.clear();
this.getStore('Warehouses').filter([
{property:'company_id', value: 11},
{property:'type', value: 'plant'}
]);
this.getStore('Warehouses').load();


I've been looking at extending the 'Warehouses'-store with context-dependant stores (I.e 'Warehouses4Combos)' and overriding load method to apply 'default' filters, but i'm not sure which is the better way of doing it in terms of proper extjs design.