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

bindWithEvent error while converting mooRainbow to mootools 1.4.4

本文关键字:bindWithEvent 错误 mooRainbow 转换 mootools      更新时间:2023-09-26

我正在尝试让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'

附言。这个问题似乎与MooTools/JS:bindWithEvent的问题相似,但那里的答案与我的上下文无关,我不确定它是否是同一个问题。

很久没有说话的伴侣(coda在这里)。 回到IRC!

你真的需要看看函数本身。 通常它们并不是真正的最佳选择,并且依赖于太多的参数和设置。

已弃用的绑定事件可能的解决方法可以是...

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 个事件函数中,参数 1 将是事件。 el == event.target - 这是一种模式

2 咖喱 ftw

匿名函数中的代理。

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

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