我面临一个与我的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