Tho*_*mas 7 jquery backbone.js
我试图将整个模型集合同步到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();
Run Code Online (Sandbox Code Playgroud)
当我尝试调用syncCollection()时,它返回以下错误:Uncaught TypeError: Object [object Object] has no method 'save'我是Backbone的新手,所以很可能是我在某个地方犯了一个相当基本的错误......
Tho*_*mas 13
好的,一壶新鲜咖啡,一天后,我明白了.我们不正确地调用sync().这是我们应该做的:
syncCollection: function() {
Backbone.sync('create', this);
}
Run Code Online (Sandbox Code Playgroud)
感谢machineghost帮助我探索这个问题 - 我完全专注于错误的事情.
| 归档时间: |
|
| 查看次数: |
9971 次 |
| 最近记录: |