Backbone.js获取未知错误

时间:2012-09-24 10:08:38

标签: backbone.js

我正面临我的backbone.js应用程序的问题:我正在尝试从JSON Web服务获取数据,GET HTTP请求是成功的(我在chrome的开发者控制台中查看)但是骨干提取触发器错误,不会更新模型。

您可以在这里查看代码: https://github.com/tdurand/faq-app-client-mobile

您可以运行该应用并尝试在此处进行调试: http://tdurand.github.com/faq-app-client-mobile/

JSON Feed就像这样

[
    {
       "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;

非常感谢你的帮助

2 个答案:

答案 0 :(得分:0)

从我看到的情况来看,你不是在任何地方制作你的收藏品的实例。 Javascript并不是真正的面向对象,它有这种奇怪的函数 - 作为类和prototype - 继承类型的交易,可以混淆来自OO -world的任何人。

var Categories = Backbone.Collection.extend...

上面的内容是您使用您定义的对象(或字典)扩展Backbone Collection(这是一个函数)原型属性。为了能够将此“类”实例化为对象,您需要使用new关键字。你没有这样做,所以你要调用'class'fetch的函数Categories,这会产生意想不到的结果。

在获取之前实例化你的集合:

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);
        }
    });
}

希望这有帮助!

答案 1 :(得分:0)

我发现了我的错误。

我的后端服务器(带有play!framework),没有正确呈现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));

     }