我面臨一個與我的backbone.js應用程序的問題:我試圖從JSON Web服務中獲取數據,GET HTTP請求是成功的(我看了一下開發者控制檯鉻),但骨幹提取觸發錯誤,並不會更新模型。Backbone.js獲取未知錯誤
你可以看看這裏的代碼: https://github.com/tdurand/faq-app-client-mobile
而且你可以運行應用程序並嘗試調試這裏: http://tdurand.github.com/faq-app-client-mobile/
JSON供稿就是這樣
[
{
"title":"Probleme ou Bug",
"desc":"Pour les problemes ou les bugs rencontrés",
"entries":[
{
"title":"testdqs",
"desc":"testqsdqs"
}
]
}
]
我的收藏模特是:
var Categories = Backbone.Collection.extend({
url:"http://cryptic-eyrie-7716.herokuapp.com/faq/fr",
model:Category,
parse:function(response) {
console.log("test")
console.log(response);
return response;
},
sync: function(method, model, options) {
var params = _.extend({
type: 'GET',
dataType: 'jsonp',
url: model.url,
processData:false
}, options);
return $.ajax(params);
},
});
而我的觀點:
var IndexView = Backbone.View.extend({
el: '#index',
initialize:function() {
Categories.fetch({
success: function(m,r){
console.log("success");
console.log(r); // => 2 (collection have been populated)
},
error: function(m,r) {
console.log("error");
console.log(r.responseText);
}
});
Categories.on('all', this.render, this);
},
//render the content into div of view
render: function(){
//this.el is the root element of Backbone.View. By default, it is a div.
//$el is cached jQuery object for the view's element.
//append the compiled template into view div container
this.$el.html(_.template(indexViewTemplate,{categories:Categories}));
//Trigger jquerymobile rendering
$("#index").trigger('pagecreate');
//return to enable chained calls
return this;
}
});
return IndexView;
非常感謝您的幫助
對不起,我使用requirejs,所以我在模塊的末尾做了Categories的實例。 你可以在這裏看到完整的視圖代碼:https://github.com/tdurand/faq-app-client-mobile/blob/master/views/category.js,我可以向你保證類別是instanciated。 我已經嘗試了相同的代碼獲取本地json文件,它的工作... – tdurand
在你的github鏈接,你_don't_初始化集合。嘗試改變第31行以返回新的CategoryView ...除非你的意思是https://github.com/tdurand/faq-app-client-mobile/blob/master/models/Categories.js – Jean
我的不好,我在這裏初始化集合(最後一行):https://github.com/tdurand/faq-app-client-mobile/blob/master/models/Categories。js 然後我加載模塊這個instanciated集合在我看來 – tdurand