为控制器中的商店事件设置侦听器 [英] Set listener for store events in a controller

查看:21
本文介绍了为控制器中的商店事件设置侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有商店、模型和一些视图的控制器.

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

我需要在控制器中监听 store 的 beforesyncwrite 事件,但是我不知道如何在控制器中设置这些监听器 控制-功能.

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?

推荐答案

你的方式是可能的,但它并不理想,IMO.更好的方法是使用控制器的 store getter.在您的情况下,代码将是这样的:

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

这篇关于为控制器中的商店事件设置侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆