我已经浏览了同一主题上的其他帖子,也许我在自己的代码中遗漏了一些东西,但在我看来,事情应该有效。我从来没有使用localStorage和骨干,似乎在这里遗漏了一些东西。非常感谢任何想法!
我的实例:
var Address = {
run: function() {
this.router = new AddressRouter();
this.contactscollection = new AddressCollection();
this.addContact = new AddressAddView();
this.listView = new AddressListView();
Backbone.history.start();
}
};
我的收藏:
var AddressCollection = Backbone.Collection.extend({
model: AddressModel,
localstorage: new Store('backbone-addressbook')
});
我的模特:
var AddressModel = Backbone.Model.extend({
defaults: {
id: '',
name: '',
email: ''
}
});
和我的观点:
var AddressAddView = Backbone.View.extend({
el: '#content',
template: _.template($('#addContactTemplate').html()),
events: { 'submit form#addContactForm': 'createContact'},
createContact: function(){
Address.contactscollection.create(this.newAttributes());
this.save();
this.input.val('');
},
newAttributes: function() {
return {
id: $('#id').val(),
name: $('#name').val(),
email: $('#email').val()
}
},
initialize: function() {
_.bindAll(this, 'addContactPage','render');
},
addContactPage: function(id) {
var contact = {},
model = Address.contactscollection.get(id);
if (id !== undefined && model !== undefined) {
contact = model.toJSON();
}
this.$el.html(this.template({contact: contact}));
}
});
答案 0 :(得分:2)
案件很重要。
localstorage: new Store('backbone-addressbook')
需要
localStorage: new Store('backbone-addressbook')
如果未设置localStorage
,则假定您的集合持久存在于RESTful API中,并且需要url
。