how to replace bindwithevent in mootools 1.3

痞子三分冷 提交于 2019-12-20 03:38:10

问题


I wonder how to replace the bindWithEvent function in Mootools 1.3, the example in the documentation is very basic:

Element.addEvent('click', function(e){
myFunction.bind(bind, [e]);});

But, what about if I need to pass a param to the event handler? This is the way in Mootools 1.2:

Element.addEvent('click', function(e, param) { e.stop(); alert(param) }.bindWithEvent(this,['text']);

Any idea on how to replace this in Mootools 1.3.

Update: I found a very ugly solution, but a least it works while I find a built-in solution:

Element.addEvent('click', function(e){ e.stop(); this.bind.myFunc(this.param);}.bind({bind:this, param: 'text'}));

回答1:


el.addEvent('click', function(event){
    myFunction(event, param1, param2); // can use .pass and bind this again
}.bind(this));

it's hard to explain why it got deprecated though.

example in a class context:

var foo = new Class({
    initialize: function(el) {
        document.id(el).addEvent('click', function(event){
            this.foo(event, "hello");
        }.bind(this));
    },
    foo: function(event, what) {
        console.log(event, this); // this is the class instance
        alert(what);
    }
});

new foo("foo");



回答2:


You should read the upgrade from 1.2 to 1.3 page: http://github.com/mootools/mootools-core/wiki/Update-from-1.2-to-1.3

This is what I came up with: http://jsfiddle.net/MBx2D/2/



来源:https://stackoverflow.com/questions/4062839/how-to-replace-bindwithevent-in-mootools-1-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!