Set listener for store events in a controller

33,299

Solution 1

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

Solution 2

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.

Solution 3

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,
               ...

Solution 4

Ext.define('Store', {
    model: 'Model',
    extend: 'Ext.data.Store',
    listeners: {
        'beforesync': function(){
            App.getController('somecontroller').onBeforeSync();
        }
    }
});

App - your application object The function onBeforeSync you can implement it in the controller ... this is the only way i could assign the event to the store and still implement the logic in the controll. I hope it helps

Solution 5

I solved it by myself.

I added the listener manually in the render-event of my Panel

Ext.getCmp('userPanel').down('gridpanel').getStore().addListener('write',this.finishedLoading, this);

Thank you for the help @nscrob.

Share:
33,299
Demnogonis
Author by

Demnogonis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live...

Updated on April 09, 2021

Comments

  • Demnogonis
    Demnogonis about 3 years

    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?