Set listener for store events in a controller

2019-01-21 22:24发布

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?

7条回答
虎瘦雄心在
2楼-- · 2019-01-21 23:02

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.

查看更多
我命由我不由天
3楼-- · 2019-01-21 23:04

Why not just relay the store's events? For example:

this.getUsersGrid().relayEvents(this.getUsersStoreStore(), ['write'], 'store')

And then be able to

this.control('somegrid-selector': {storeWrite: function(){...}})

查看更多
beautiful°
4楼-- · 2019-01-21 23:08

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,
               ...
查看更多
Anthone
5楼-- · 2019-01-21 23:13

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.

查看更多
Fickle 薄情
6楼-- · 2019-01-21 23:17

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
    });
},
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-21 23:17
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

查看更多
登录 后发表回答