how to replace bindwithevent in mootools 1.3

I need covert this code from mootools 1.2 to 1.3


var SomeClass = new Class({
initialize: function (els) {
for (var i = 0; i < els.length; i++) {
els[i].addEvent('click',
this.alert.bindWithEvent(this, [i, els[i].get('text')])
);
}
},

alert: function (event, index, text) {
alert(
index + ' -> ' + text + ' | ' +
'x:' + event.page.x + ', y:' + event.page.y
);
}
});

Here is the working version (1.2) http://jsfiddle.net/9Pn99/
Here is my version for 1.3 http //jsfiddle.net/9Pn99/1/

EDIT: I figured out how to do it, with a closure. http://jsfiddle.net/9Pn99/4/


for (var i = 0; i < els.length; i++) {
(function (j) {
els[i].addEvent('click',
function (e) {
this.alert(e, j);
}.bind(this)
);
}.pass([i], this))();
}


Is there a better solution?

EDIT2: I found another easy way:

els.each(function (el, i) {
els[i].addEvent('click',
function (e) {
this.alert(e, i);
}.bind(this)
);
}, this);

Looks like I'm talking alone.


I wonder how to replace the bindWithEvent funtion 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'}));


转自:http://stackoverflow.com/questions/4062839/how-to-replace-bindwithevent-in-mootools-1-3
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值