2012-09-24 57 views
0

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

非常感谢您的帮助

回答

0

我发现我的错误。

我的后端服务器(带玩!框架),并没有渲染JSONP正确

这是代码我现在用做它,如果有人有相同的问题。

//Render JSONP 
    if (request.params._contains("callback")) { 

     Gson gson = new Gson(); 

     String out = gson.toJson(categoryJSON(categories,lang)); 

     renderText(request.params.get("callback") + "(" + out + ")"); 

    } else { 

     renderJSON(categoryJSON(categories,lang)); 

    } 
0

从我所看到的,你不是让你收集的一个实例的任何地方。 Javascript并不是真正的面向对象,它有这种奇怪的功能 - 类和交易类型,可以混淆来自OO世界的任何人。

var Categories = Backbone.Collection.extend... 

以上会发生什么情况是,你延伸骨干Collection的(这是一个函数)的原型的属性与对象(或字典)定义。为了能够将这个'class'实例化为一个对象,你需要使用new关键字。你不这样做,所以你正在调用'类'Categories的函数fetch,这将产生意想不到的结果。

取之前那么实例化您的收藏:

initialize:function() { 
    this.collection = new Categories(); // instantiate the collection 

    // also set event handlers before producing events 
    this.collection.on('all', this.render, this); 

    this.collection.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); 
     } 
    }); 
} 

希望这有助于!

+0

对不起,我使用requirejs,所以我在模块的末尾做了Categories的实例。 你可以在这里看到完整的视图代码:https://github.com/tdurand/faq-app-client-mobile/blob/master/views/category.js,我可以向你保证类别是instanciated。 我已经尝试了相同的代码获取本地json文件,它的工作... – tdurand

+0

在你的github链接,你_don't_初始化集合。尝试改变第31行以返回新的CategoryView ...除非你的意思是https://github.com/tdurand/faq-app-client-mobile/blob/master/models/Categories.js – Jean

+0

我的不好,我在这里初始化集合(最后一行):https://github.com/tdurand/faq-app-client-mobile/blob/master/models/Categories。js 然后我加载模块这个instanciated集合在我看来 – tdurand