我正在尝试设置我的集合以侦听集合内模型上触发的事件,如下所示:
var Collection = Backbone.Collection.extend({
initialize: function() {
this.on('playback:completed', this.playNext);
},
playNext : function() { }
});
在我的测试中,我将新的 Backbone.Models 添加到集合的一个实例中,然后触发playback:completed
它们......并且playNext
没有被调用。如何正确设置?
编辑:添加测试代码(使用 Jasmine):
var collection;
describe('Collection', function() {
beforeEach(function() {
collection = new Collection();
});
it('should playNext when playback:completed is triggered', function() {
var model1 = new Backbone.Model();
var model2 = new Backbone.Model();
var spy = spyOn(collection, 'playNext').andCallThrough();
collection.add(model1);
collection.add(model2);
model1.trigger('playback:completed');
expect(spy).toHaveBeenCalled();
});
});