Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set listener for store events in a controller

I have a controller with a store, a model, and some views.

I need to listen for the beforesync and write event of the store in the controller, but I don't know how to set these listeners in the controllers control-function.

My store looks like this :

Ext.define('DT.store.UsersStore', {     extend : 'Ext.data.Store',     model : 'DT.model.User',     id : 'myStore'     autoSync : true,     proxy : {         type : 'ajax',         api : {             read : '/load_entries',             update : '/update_entry'         },         reader : {             type : 'json',             root : 'user',             successProperty : 'success'         }     } }); 

Now I try to listen to the events in my controller :

... init : function () {     this.control({         'myStore' : {             beforesync : this.doSomething,             write : this.doSomethingElse         }     }); }, ... 

My expected result is that the functions will be executed, when the events are fired. But at this time nothing happens when they are fired.

How can I get this to work?

like image 701
Demnogonis Avatar asked Aug 25 '11 15:08

Demnogonis


People also ask

What does a listener do in event handling?

An event listener is a procedure or function in a computer program that waits for an event to occur. Examples of an event are the user clicking or moving the mouse, pressing a key on the keyboard, disk I/O, network activity, or an internal timer or interrupt.

What is the difference between an event handler and an event listener?

Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.

How do I find an event listener?

Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element. You can expand any event listener by clicking the right-pointing arrowhead.

Can event listeners be nested?

The final step, the callback function, can be written as a nested anonymous function inside the event listener or can be a designated function fully defined in a separate function. The callback handles the resulting work you want to happen after an event has occurred.


1 Answers

Your way is possible but it's not ideal, IMO. The better way is to use controllers's store getter. In your case the code would be something like this:

init : function () {     // every controller has getters for its stores.     // For store UsersStore getter would be getUsersStoreStore()     this.getUsersStoreStore().addListener('write',this.finishedLoading, this);     this.control({         // widgets event handlers     }); }, 
like image 171
Molecular Man Avatar answered Sep 28 '22 17:09

Molecular Man