我剛剛開始使用backbone.js。我在從服務器獲取數據時遇到問題。這是我從服務器獲得的響應。Backbone.js:無法將同一個模型添加到集合中兩次
[{
"list_name":"list1",
"list_id":"4",
"created":"2011-07-07 21:21:16",
"user_id":"123456"
},
{
"list_name":"list2",
"list_id":"3",
"created":"2011-07-07 21:19:51",
"user_key":"678901"
}]
這裏是我的javascript代碼...
// Router
App.Routers.AppRouter = Backbone.Router.extend({
routes: {
'': 'index'
},
initialize: function() {
},
index: function() {
var listCollection = new App.Collections.ListCollection();
listCollection.fetch({
success: function() {
new App.Views.ListItemView({collection: listCollection});
},
error: function() {
alert("controller: error loading lists");
}
});
}
});
// Models
var List = Backbone.Model.extend({
defaults: {
name: '',
id: ''
}
});
App.Collections.ListStore = Backbone.Collection.extend({
model: List,
url: '/lists'
});
// Initiate Application
var App = {
Collections: {},
Routers: {},
Views: {},
init: function() {
var objAppRouter = new App.Routers.AppRouter();
Backbone.history.start();
}
};
我得到的錯誤在Backbone.js的
if (already) throw new Error(["Can't add the same model to a set twice", already.id]);
「不能在同一個模型添加到一組兩次」上這條線
我檢查了Backbone.js註釋並發現第一個模型被添加到集合中,但第二個模型給出了這個錯誤。這是爲什麼發生?我應該改變服務器端響應中的某些內容嗎?
救了我很多麻煩 - 謝謝! –