将mooRainbow转换为mootools 1.4.4时出现bindWithEvent错误

时间:2012-04-12 19:49:58

标签: javascript events mootools

我试图让mooRainbow 1.2b(http://moorainbow.woolly-sheep.net/)使用mootools 1.4.4并且我遇到了一个我似乎无法找到答案的错误。

本节中引发了错误...


OverlayEvents: function ()
{
    var lim, curH, curW, inputs;
    curH = this.snippet('curSize', 'int').h;
    curW = this.snippet('curSize', 'int').w;

    // $A is deprecated, the original line here was:
    // inputs = $A(this.arrRGB).concat(this.arrHSB, this.hexInput);

    inputs = this.arrRGB.concat(this.arrHSB, this.hexInput);

    document.addEvent('click', function ()
    {
        if (this.visible) this.hide(this.layout);
    }.bind(this));

    inputs.each(function (el)
    {

        // this is where the error is thrown
        el.addEvent('keydown', this.eventKeydown.bindWithEvent(this, el));
        el.addEvent('keyup', this.eventKeyup.bindWithEvent(this, el));

    }, this);
    [this.element, this.layout].each(function (el)
    {
        el.addEvents({
            'click': function (e) { new Event(e).stop(); },
            'keyup': function (e)
            {
                e = new Event(e);
                if (e.key == 'esc' && this.visible) this.hide(this.layout);
            }.bind(this)
        }, this);
    }, this);

这是抛出的错误......

Uncaught TypeError: Object function (){
if (method.$protected && this.$caller == null) 
throw new Error('The method "' + key + '" cannot be called.');
var caller = this.caller, current = this.$caller;
this.caller = current; this.$caller = wrapper;
var result = method.apply(this, arguments);
this.$caller = current; this.caller = caller;
return result;
} has no method 'bindWithEvent'

PS。问题似乎与MooTools/JS: bindWithEvent中的问题类似,但那里的答案与我的背景无关,我不确定它是否是同一个问题。

1 个答案:

答案 0 :(得分:0)

很长一段时间没有说话的伴侣(在这里结尾)。回到IRC!

你真的需要看看funcs本身。通常它们并不是最佳的,并且依赖太多的论据和设置。

已弃用的bindWithEvent的可能解决方法可以是......

1替换此...

el.addEvent('keydown', this.eventKeydown.bindWithEvent(this, el));
el.addEvent('keyup', this.eventKeyup.bindWithEvent(this, el));

...有类似的东西:

el.addEvents({
    keyup: this.eventKeyup.bind(this),
    keydown: this.eventKeydown.bind(this)
});

然后在2个事件funcs中,参数1将是事件。 el == event.target - 这是一种模式

2 curry ftw

匿名函数中的代理。

var self = this;
el.addEvent("keyup", function(e) {
    self.eventKeyup(e, this);    
});

3 ......等天空是极限。