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

标签 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但那里的答案与我的上下文无关,我不确定这是否是同一个问题。

最佳答案

好久没说话了,伙计(这里是尾声)。返回 IRC!

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

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

2 curry ftw

匿名函数中的代理。

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

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

关于javascript - 将 mooRainbow 转换为 mootools 1.4.4 时出现 bindWithEvent 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10130708/

相关文章:

javascript - 将特定参数传递给函数

javascript - AngularJS 实现模板本地化

events - 从 HDFS 接收文件系统事件和通知

javascript - 如何在 Mootools 中向对象创建的元素添加事件?

javascript - 了解 Mootools 中的 $

javascript - 在 FolderIterator 和 FileIterator 上循环 - Google Apps 脚本

javascript - 未使用的变量设置为什么?

.net - RowLeave 上的 Datagridview 没有为 boolean 值分配正确的值

javascript - 如何从 jQuery 事件对象中检索 keyCode 和 charCode?

javascript - mootools.swiff : loaderInfo never completes