Backbone解析ajax响应函数未被调用

时间:2013-11-30 19:37:44

标签: javascript jquery ajax rest backbone.js

我有一个我想要获取的骨干集合和模型数组。当我调用fetch时,ajax请求会命中正确的rest端点。但是,我的集合的解析函数永远不会被调用。我不确定json最终会在哪里,但我确实在chrome的网络选项卡中获得了带有效负载的200。我应该这样做吗?我过去做过这个(主干1.0,现在主干1.1)并且它工作正常,所以我不确定问题是什么?

 new MonitorCollection(app);
 this.collection.fetch(); // this makes the correct ajax call

收集:

define([
  'jquery',
  'underscore',
  'backbone',
  'model/monitormodel',
], function ($, _, Backbone, MonitorModel) {    

    var collection = Backbone.Collection.extend({
        initialize: function (app, options) {
            this.app = app;
        },
        url: "api/monitor?since=10",
        model: MonitorModel,

        parse: function (response) {

            console.log(response);
            return response;
        }

    });
    return collection;

});

型号:

define([
  'jquery',
  'underscore',
  'backbone',
  'view/monitordeviceview',
], function ($, _, Backbone, MonitorDeviceView) {

    var model= Backbone.Model.extend({
        initialize: function (app, options) {
            this.app = app;
            this.view = new MonitorDeviceView(this.app);
            this.view.render(this);
        },

        parse: function (response) {

            console.log(response);
            return response;
        }

    });
    return model;

});

1 个答案:

答案 0 :(得分:4)

问题是被回送的json无效json:NaN在json中无效。

{
        "deviceId": "ac867418c110",
        "totalProbeRequestCount": 0,
        "errorRate": NaN,
        "errorCount": 0,
        "succcesRate": NaN,

要帮助调试此添加错误处理程序并阅读链接:

这些链接应该有助于了解骨干网与服务器的交互方式。

    var collection = Backbone.Collection.extend({
        initialize: function (options) {
            this.on("error", this.error, this)
            this.fetch();
        },
        url: "api/monitor?since=10",
        model: MonitorModel,

        parse: function (data) {
            console.log(data);
            return data.items;
        },

        error: function (model, response, options) {
            console.log(model);
            console.log(response);
            console.log(options);
        }

    });