2012-09-24 79 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