3
votes

I have a requirement in my application where in I need to show the UI components(like text field, combo box) based on the values i get from the server side. To be precise, I have a combobox on the page, when the user changes the values i need to send the selected value to server to get information on what needs to be displayed. Without using MVC i implemented it as below

  1. When the user changes the value in the combobox, i refresh(using load method) another store
  2. When i get the data back from the server('datachanged' event) i read the data and create the UI components

Now I am trying to use ExtJS MVC, so i have two questions

  1. How do I access a store which is associated to a controller but not necessarily to any UI components from the view
  2. How can I setup the events of a store in the controller(in the control function) just like we setup events from view

Code i want to setup events from Store like 'datachanged' in controller like below -

this.control({
    'viewport > #content-panel' : {
        render : this.createMainTabs
     }
});
2

2 Answers

2
votes

In addition to sha's reply I can say that you may setup your store eventhandlers in your controller. For example you have a combobox with a store, so you could write like this:

this.control({
    '#myCombo' : {
        afterrender : this.setupStoreListeners
     }
});

....

setupStoreListeners: function(combo){
    var store = combo.store;
    store.on('datachanged', //.....);
}

And one more thing, as sha wrote you could always get store by name, but I use this only when I need to share a store instance between multiple controllers. If I need this store only in one controller, I just save it inside this controller:

setupController: function(){
   this.myStore = this.getCombo().store; // now you could use this.myStore anywhere you need

}
1
votes

To get store object in the controller just use this.getStore('StoreName'), each store by default has its own instance and it doesn't really matter whether it's binded to any UI component you have.

After you get store object you can subscribe to any store events using any method you prefer. I usually like

store.on('load', {...})

Also I would not change anything in UI from the view code. I would put all this customization into controller code.