有這樣的問題很多。我還沒有找到答案。TodoMVC與骨幹和要求不能從服務器獲取
使用示例TodoMVC與backbone.js和require.js,我想從服務器而不是從本地存儲提取。
我有一個返回正確的json的集合,其中模型是這樣的一個網址:
{"string1": "foo", "string2":"bar", "somefloat":0}
在我的模型(模型/ todo.js)我改變默認爲:
defaults: {string1: '', string2: '',somefloat: 0},
和我的收藏(collections/todos.js)我註釋掉localstorage並添加一個url。
這使得抓取到我的服務器,我可以看到它返回的JSON集合。
但由於某些原因,該模型是在Backbone.js的行未定義817
// Prepare a model or hash of attributes to be added to this collection.
_prepareModel: function (model, options) {
options || (options = {});
if (!(model instanceof Model)) {
console.log(Model);
var attrs = model;
console.log(attrs.Kana);
options.collection = this;
//ERROR IN THIS LINE: Uncaught TypeError: undefined is not a function
model = new this.model(attrs, options);
if (!model._validate(model.attributes, options)) model = false;
} else if (!model.collection) {
model.collection = this;
}
return model;
},
爲什麼當我不使用本地存儲的模型不確定?
你也評'「的lib /骨幹網/ localStorage的」,'在函數的定義您的收藏?因爲我知道Backbone Localstorage只是替換默認的Backbone.Sync()函數,所以如果你嘗試使用經典的ajax方法來獲取數據,它將無法工作。 – Ingro
感謝您的回覆。在collections/todos中,我註釋了''lib/backbone/localstorage''和'localStorage:new Store('todos-backbone')',然後我插入了'url:'/ Practice/GetCollection','I也嘗試在集合中添加'initialize:function(){this.model = Todo;}''沒有什麼區別 – Carsten
你是否在模型中添加了'urlRoot'屬性?應該是:'urlRoot: '/ Practice/GetCollection'' – Ingro