我的模型看起来像这样:
var TodosModel = Backbone.Model.extend({
defaults: {
id: null,
content: 'Something Todo',
completed: false
},
url: function() { return 'api/'+this.id; }
});
我正在通过以下方式添加模型:
var todoID = _.uniqueId();
var todoContent = this.newTodoField.val();
var todoCompleted = false;
// Ensure there's something to save
if(todoContent.length>0){
var _this = this;
// Create model
var todo = new TodosModel({
id: todoID,
content: todoContent,
completed: todoCompleted
});
todo.save({}, {
wait: true,
success: function(model, response) {
// Let the events deal with rendering...
_this.collection.add(model);
},
error: function(model, response) {
console.log('Could not create todo');
}
});
}
我遇到的问题是,由于某种原因,每个id都会加倍增加 - 所以如果我从没有元素开始,我会得到1,3,5,7 ......
哪个好,除非我重新加载并且这些ID是从API引入的,然后下一个生成的_.uniqueID基于渲染的计数。
非常感谢任何帮助,这里是完整的代码:http://sandbox.fluidbyte.org/todos/js/todos.js