引发一系列事件 Backbone event:name [英] Cause an array of events Backbone event:name
问题描述
_.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);
});
我希望所有事件都通过掩码 alert:.
I want сall all events are tied to this object with mask alert:.
object.trigger("myalert", "param");
推荐答案
Backbone.Events
中没有任何命名空间,但您可以添加自己的命名空间.例如,像这样:
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);
}
};
允许你说 obj.trigger_matching(/^myalert:/, 1, 2, 3)
并且正确的事情会发生.
would allow you to say obj.trigger_matching(/^myalert:/, 1, 2, 3)
and The Right Thing would happen.
演示:http://jsfiddle.net/ambiguous/p8p5R/
这将触发多个 'all'
事件(每个 this.trigger.apply
一个),这可能是您想要的,也可能不是.如果不是,则将 this.trigger.apply
调用替换为标准 trigger
这样你最多可以触发一个 'all'
事件.
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.
这篇关于引发一系列事件 Backbone event:name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!