在ExtJS 5.1中触发自定义事件

时间:2015-08-11 20:41:08

标签: javascript extjs

当使用模态窗口成功上传文件时,我一直在尝试触发自定义事件。主页面上的网格侦听事件,并在成功上载文件时重新加载其存储。问题是,网格永远不会捕获此事件。

我认为我对自定义事件的工作方式存在根本性的误解。我应采取哪些步骤才能重回正轨?

SomeCommonUtilityClass.js

upload: function(args) {
    Ext.create('Ext.window.Window', {

    /* form with some controls */
        buttons: [{
            text:'Upload',
            handler: function() {
                var win = this.up('window');
                var form = this.up('form').getForm();
                form.submit ({
                    url: myAjaxCall,
                    success: function() {
                        /* fire event here */
                        win.fireEvent('uploadSuccess');
                    },
                    failure: function() {
                        /*...*/
                    }
                });
            }
        }, 
    /* etc. */
    });
}

SomeOtherFileView.js

{
    xtype:'grid',
    itemId:'uploadedGrid',
    listeners: {
        uploadSuccess: 'reloadUploadStore'
    },
    bind: {
        store:'{form}'
    },
    columns:[/*...*/]
}

SomeOtherFileViewController.js

reloadUploadStore: function() {

    console.log("My event fired!") // Never gets here.
    /* .... */
    store.load({
        params: ({
            a: "a",
            b: "b"
        });
        callback: function() {
            /* do more stuff */
        }
    });
}

1 个答案:

答案 0 :(得分:2)

<强> SomeCommonUtilityClass

win.fireEvent('uploadSuccess');

自定义事件和侦听器的示例:

<强> SomeOtherFileViewController

init: function() {
        this.listen({
            // We are using Controller event domain here
            controller: {
                // This selector matches any originating Controller
                '*': {      
                    uploadSuccess: 'reloadUploadStore'
                }
            }
        });
    },
    reloadUploadStore: function() {
        //your code
    }

或者如果你想传递一个参数:

win.fireEvent('uploadSuccess',extraArgument);

控制器代码是一样的。只有你的功能定义发生了变化:

reloadUploadStore: function(yourArgument) {
    //Do your stuff with extraArgument
}