在Backbone集合上使用.on()获取多个事件时获取事件名称.

Ben*_*son 5 backbone.js backbone-events

我如何知道在使用.on()将多个事件绑定到Backbone集合时触发了什么事件?有关说明,请参阅以下示例.(另见jsFiddle:http://jsfiddle.net/PURAU/3/)

var Car = Backbone.Model.extend({
    nrOfWheels: 4,
    color: 'red',
    speed: 'slow'
});

var Garage = Backbone.Collection.extend({
    model: Car
});

var myGarage = new Garage(),
    myCar = new Car();

myGarage.on('add change reset', function() {
    // How do I know what event was triggered?
    console.log('add change reset', arguments);
});

myGarage.on("all", function() {
    // In here, the name of the event is the first argument.
    console.log('all', arguments);
});

// Trigger add
myGarage.add(myCar);

// Trigger change
myCar.set('speed', 'fast');

// Trigger reset
myGarage.reset();
Run Code Online (Sandbox Code Playgroud)

CD.*_*D.. 0

您需要重写trigger方法来执行此操作,请查看代码:

https://github.com/documentcloud/backbone/blob/master/backbone.js#L148