Search code examples
javascriptmodel-view-controllerextjsextjs4

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?


Solution

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