次のコードではadd
、ビューにバインドされたイベントが2回発生します(コレクションに要素を一度に追加するとさらに発生します)。
http://jsfiddle.net/radu/GnG66/
App = window.App || {};
var Model = Backbone.Model.extend();
var Collection = Backbone.Collection.extend();
App.collection = new Collection({ model: Model });
var View = Backbone.View.extend({
events: {
'click': function() {
console.log('click');
App.collection.add([{
foo: 'foo'
}, {
bar: 'bar'
}]);
}
},
initialize: function() {
App.collection.on('add', function() {
console.log('Something has been added to the collection')
}, this);
}
});
$(function() {
App.view = new View({ el: '#test' });
});
コレクションに配列を追加する代わりに、引数として複数のオブジェクトを渡すだけの場合(基本的には角かっこを削除するだけ)、イベントは1回だけ発生します。
{ silent : true }
これは仕様によるものであり、オプションとして渡さずにこの動作をオーバーライドする方法はありますか?