原因事件骨干事件的数组:名称 [英] Cause an array of events Backbone event:name

查看:97
本文介绍了原因事件骨干事件的数组:名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  _扩展(对象,Backbone.Events);object.on(myalert:一是功能(MSG){
  document.body.innerHTML + =(eve1+消息:='+味精+';名称:='+ this.name);
},上下文);
object.on(myalert:二,函数(MSG){
  document.body.innerHTML + =(eve2+消息:='+味精+';名称:='+ this.name);
});

我要сall所有事件都依赖于这个对象与面具的警告:

  object.trigger(myalert,参数);


解决方案

有没有 Backbone.Events 的命名空间,但你可以添加你自己的。例如,这样的事情:

  obj.trigger_matching =功能(重新){
    变参= [''] .concat([] splice.call(参数,1));
    对(在this._events名){
        如果(!name.match(RE))
            继续;
        ARGS [0] =名称;
        this.trigger.apply(这一点,参数);
    }
};

将允许你说 obj.trigger_matching(/ ^ myalert:/,1,2,3)。和正确的事情会发生。

演示: http://jsfiddle.net/ambiguous/p8p5R/

这将引发多个全部事件(每个 this.trigger.apply ),这可能会或可能不是你想要的。如果没有,那么替换 this.trigger.apply 与标准的 触发 ,这样就可以在最多一个全部事件。

_.extend(object, Backbone.Events);

object.on("myalert:one", function(msg) {
  document.body.innerHTML+=("eve1 " + ' msg:= '+msg+ ' ;name:= '+this.name);
},context);


object.on("myalert:two", function(msg) {
  document.body.innerHTML+=(" eve2 " + ' msg:= '+msg+ ' ;name:= '+this.name);
});

I want сall all events are tied to this object with mask alert:.

   object.trigger("myalert", "param");

解决方案

There isn't any namespacing in Backbone.Events but you could add your own. For example, something like this:

obj.trigger_matching = function(re) {
    var args = [''].concat([].splice.call(arguments, 1));
    for(name in this._events) {
        if(!name.match(re))
            continue;
        args[0] = name;
        this.trigger.apply(this, args);
    }
};

would allow you to say obj.trigger_matching(/^myalert:/, 1, 2, 3) and The Right Thing would happen.

Demo: http://jsfiddle.net/ambiguous/p8p5R/

That will trigger multiple 'all' events (one for each this.trigger.apply) which may or may not be what you want. If it isn't then replace the this.trigger.apply call with a custom version of the standard trigger so that you can trigger at most one 'all' event.

这篇关于原因事件骨干事件的数组:名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆