我正在尝试将整个模型集合同步到backbone.js 中的服务器。我想将一个 JSON 包发布到包含所有集合模型属性的服务器。这应该很简单,但我不确定我做错了什么:
var RegisterCollection = Backbone.Collection.extend({
initialize: function () {_(this).bindAll('syncCollection');},
model: RegisterModel,
url: 'api/process.php',
updateModel: function(registerModel) {
var inputName = '#'+registerModel.get('inputName');
var userInput = $(inputName).val();
registerModel.set({value: userInput});
},
syncCollection: function(registerModel) {
this.sync();
}
});
var RegisterCollectionView = Backbone.View.extend({
el: "#register",
events: {
"click #submit" : "validate"
},
validate: function(e) {
e.preventDefault();
$("#register").valid();
if ($("#register").valid() === true) {
this.updateModels();
this.collection.syncCollection();
}
},
updateModels: function (){
this.collection.forEach(this.collection.updateModel, this);;
},
initialize: function(){
this.collection.on('reset', this.addAll, this);
},
addOne: function(registerModel){
var registerView = new RegisterView({model: registerModel});
this.$el.append(registerView.render().el);
},
addAll: function(){
this.$el.empty();
this.collection.forEach(this.addOne, this);
this.$el.append('<button type="submit" class="btn-primary btn" id="submit" style="display: block;">Submit</button>');
},
render: function(){
this.addAll();
return this;
}
});
var registerCollection = new RegisterCollection();
registerCollection.fetch();
var registerCollectionView = new RegisterCollectionView({collection: registerCollection});
registerCollectionView.render();
当我尝试调用 syncCollection() 时,它返回以下错误:Uncaught TypeError: Object [object Object] has no method 'save'
我是 Backbone 的新手,所以很可能我在某处犯了一个相当基本的错误......