如何删除我的集合中的所有模型(保留在本地存储中)?
模型是从本地存储中获取的——我希望模型在客户端和本地存储中都被销毁。
// Model + Collection
App.Models.Task = Backbone.Model.extend({
defaults: {
text: 'N/A'
}
});
App.Collections.Tasks = Backbone.Collection.extend({
model: App.Models.Task,
localStorage: new Backbone.LocalStorage("task")
});
// Create collection and fetch tasks
var tasks = new App.Collections.Tasks();
tasks.fetch(); // collection is now populated with 4 tasks
// Delete all models (both at client and local storage)
tasks.each(function(model) {
model.destroy();
})
从运行它开始,我只破坏了一些模型 - 发生此错误并防止其余模型被破坏:
Uncaught TypeError: Cannot read property 'destroy' of undefined
非常感谢您对此的任何帮助!