Set listener for store events in a controller Set listener for store events in a controller javascript javascript

Set listener for store events in a controller


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    });},


Here is an alternative syntax to Molecular Man's answer.

Instead of writing,

init : function () {    this.getUsersStoreStore().addListener('write',this.finishedLoading, this);    this.control({        // widgets event handlers    });},

You can write

init : function () {    this.getUsersStoreStore().on({        write: this.finishedLoading,        scope: this    });    this.control({        // widgets event handlers    });},

I think this alternative definition reads a little bit better.

I took this from an answer Izhaki gave me.


As for Extjs 4.2.1, your initial way of accessing the store listener would actually work, if you were using the 'storeId' instead of the id and the 'listen' function instead of 'control':

Ext.define('DT.store.UsersStore', {    extend : 'Ext.data.Store',    model : 'DT.model.User',    storeId : 'myStore'    ....init : function () {    this.listen({        store: {           '#myStore' : {               beforesync : this.doSomething,               ...