Cause an array of events Backbone event:name

前端 未结 1 701
甜味超标
甜味超标 2021-01-16 20:13
_.extend(object, Backbone.Events);

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


        
相关标签:
1条回答
  • 2021-01-16 20:26

    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.

    0 讨论(0)
提交回复
热议问题