1
我正嘗試在屬於集合的Backbone模型上手動觸發change
事件。下面是一個小例子:(JSFiddle)如何手動觸發Backbone模型上的change事件?
var PersonModel = Backbone.Model.extend({});
var PersonCollection = Backbone.Collection.extend({
model: PersonModel
});
var person = new PersonModel({ name: 'Spike' });
var people = new PersonCollection([person]);
person.trigger('change'); // throws error
console.log('triggered event successfully');
,但我得到以下錯誤,當我觸發事件:(在的jsfiddle,打開javascipt的控制檯來查看完整的錯誤)
Uncaught TypeError: Cannot read property 'previousAttributes' of undefined
如何我能解決這個問題嗎?
(我這樣做的原因是,在實際的代碼模型具有收藏 - 比如person.things = new ThingCollection();
- 我想手動觸發person
的change
事件時,其收藏的變化)