jquery - MooTools 版本的 jQuery 命名空间事件?

标签 jquery namespaces mootools mootools-events

jQuery 可以让你做类似的事情

$div.bind('click.namespace', function)
$div.unbind('click.namespace')

MooTools 中有类似的工具吗?我尝试定义 pseudo event :

Event.definePseudo('shim', function(split, fn, args) {fn.apply(this, args);})

document.body.addEvent('click', function(){alert('regular click');});
document.body.addEvent('click:shim', function(){alert('shim click');});

document.body.removeEvents('click') // removes both!
document.body.removeEvents('click:shim') // removes neither!

这似乎是一个死胡同。

最佳答案

我使用 Element.implement 实现了它,但我的问题是要求使用 native MooTools 方法来实现它,而无需定义我自己的函数。

Element.implement({
    // Call as element.bind('event.namespace', function() {});
    bind: function(name, funktion) {
        // Get event type and namespace
        var split = name.split('.'),
            eventName = split[0],
            namespace = split[1];

        // Store the event by its full name including namespace
        this.bindCache = this.bindCache || {};

        if(this.bindCache[name]) {
            this.bindCache[name].push(funktion);
        } else {
            this.bindCache[name] = [funktion];
        }

        // Bind the function to the event
        this.addEvent(eventName, funktion);
    },

    // Call as element.unbind('event.namespace');
    unbind: function(name) {
        // Unbind the specified event
        var eventName = name.split('.')[0],
            funktions = this.bindCache[name],
            x = 0,
            funktion;

        for(; funktion = funktions[x++]; ) {
            this.removeEvent(eventName, funktion);
        }
    }
});

document.body.bind('click.1', function() {alert('click 1');});
document.body.bind('click.1', function() {alert('click 1 other');});
document.body.bind('click.2', function() {alert('click 2');});

document.body.unbind('click.1'); // leaves click.2

关于jquery - MooTools 版本的 jQuery 命名空间事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7220110/

相关文章:

javascript - 按下另一个按钮后按钮切换类保持事件状态

ruby - 如何在不手动加载类的情况下获取命名空间中的所有类?

c++ - 如何在 Thrift 中跨命名空间访问变量

c# - 如何在 mootools 中传递对象本身(就像我们在 C# 中所做的那样)?

events - MooTools – 单击 anchor 将类添加到页面,并延迟加载 anchor 链接

javascript - 有没有办法使用 mootools 在鼠标接近其边界时使窗口滚动?

javascript - 连接功能的多个元素

javascript - 在页面加载时替换 css 类

php - 使用自定义存储库和包使用 Composer 设置项目

jquery - JQuery Mobile 中的清除文本字段