最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ExtJs manually firing Click event, button param is different from mouse click - Stack Overflow

programmeradmin1浏览0评论

So, I have a login controller, you can click login with mouse or press Enter key, like this:

Ext.define('My.controller.Login', {
    extend: 'Ext.app.Controller',

    init: function(application) {
        this.control({
            "#idLogin button": {click: this.onButton},
            "#idLogin form > *": {specialkey: this.onKey}
        });
    },

    onButton: function(button, e, eOpts) {
        var win = button.up('window'); // the login window
        //do more stuff...
    },

    onKey: function (field, el) {
        if (el.getKey() == Ext.EventObject.ENTER) //ENTER key performs Login
            Ext.getCmp('#idLogin button').fireEvent('click');
    }
});

I realised when I use the mouse to click the Login button, onButton function works properly, button.up() returns me the Login window.

However, if I pressed Enter key and fires the onKey function to do fireEvent('click'), in this case the onButton fires up but parameter button NOT the same as the button parameter received when you click by mouse! And this time, button.up() function is undefined.

Question is, why does fireEvent('click') give me a different button parameter?

So, I have a login controller, you can click login with mouse or press Enter key, like this:

Ext.define('My.controller.Login', {
    extend: 'Ext.app.Controller',

    init: function(application) {
        this.control({
            "#idLogin button": {click: this.onButton},
            "#idLogin form > *": {specialkey: this.onKey}
        });
    },

    onButton: function(button, e, eOpts) {
        var win = button.up('window'); // the login window
        //do more stuff...
    },

    onKey: function (field, el) {
        if (el.getKey() == Ext.EventObject.ENTER) //ENTER key performs Login
            Ext.getCmp('#idLogin button').fireEvent('click');
    }
});

I realised when I use the mouse to click the Login button, onButton function works properly, button.up() returns me the Login window.

However, if I pressed Enter key and fires the onKey function to do fireEvent('click'), in this case the onButton fires up but parameter button NOT the same as the button parameter received when you click by mouse! And this time, button.up() function is undefined.

Question is, why does fireEvent('click') give me a different button parameter?

Share Improve this question edited Apr 22, 2013 at 11:24 Tom asked Apr 22, 2013 at 11:18 TomTom 16.2k15 gold badges74 silver badges115 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 19

You must use the fireEvent function like that:

var myBtn = Ext.getCmp('#idLogin button');

myBtn.fireEvent('click', myBtn);

Give it a try.

Because the button click event is a synthetic event fired by the framework. It passes along the button instance and an event object. fireEvent means "notify any subscribers that this event has happened, with these arguments", not "trigger a click event on the underlying button".

So you'd need to use:

button.fireEvent('click', button);

However, this doesn't really make sense, you're just adding an extra layer of indirection.

Why not abstract it out:

Ext.define('My.controller.Login', {
    extend: 'Ext.app.Controller',

    init: function(application) {
        this.control({
            "#idLogin button": {click: this.onButton},
            "#idLogin form > *": {specialkey: this.onKey}
        });
    },

    onButton: function(button, e, eOpts) {
        this.doWindowFoo();
    },

    onKey: function (field, el) {
        if (el.getKey() == Ext.EventObject.ENTER) //ENTER key performs Login
            this.doWindowFoo();
    },

    doWindowFoo: function() {
        // Assumes the window has an id idLogin, there are several other ways to get a reference
        var win = Ext.getCmp('idLogin');
    }
});

Use:

var button= Ext.getCmp('#idLogin button');    
button.fireHandler();

this will call the handler function in your button, in my case it worked, due i override that button handler with additional functionality and parameters value changes...(extjs 4.1.1)

Ext.getCmp('#idLogin button').handler();

A more general way:

document.querySelector("what-ever-el-selector").click();

Tested on extjs 4.2.6

Cheers

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论