【问题标题】:Backbone collection working with Rest not working与 Rest 一起工作的骨干收集不起作用
【发布时间】:2015-10-27 16:04:37
【问题描述】:

当我实际使用console.log(this.collection.fetch()) 时,您可以在responceJSON 和responseText 中看到它从api 返回了正确的数据。

但是,当我运行时,我的 readyState 始终保持为 1

this.collection.fetch({
    success : function() {
         console.log(that.collection.toJSON())
    }
});

即使我们没有服务器响应数据,它也会返回一个空白数组。

这是我的代码...

型号

define(['backbone'], function(backbone){

    var google_place = Backbone.Model.extend({

        defaults : {
            'title'     : 'no titile',
            'sub_title' : 'no subtitle',
            'content'   : 'no content available',
            'lat'       :  0,
            'lng'       :  0,
            'text'      : 'no text'
        }

    });

    return google_place;


});

收藏

define(['../model/google_place'], function(google_place){

    var google_places = Backbone.Collection.extend({

        model: google_places,

        initialize: function(models, options) {

        },

        url: function() {
            return "http://globallcoach-app.dev/api/";
        },

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

    });

    return google_places;

});

查看

define(includes(), function(backbone, mustache){

    var index = Backbone.View.extend({

        initialize : function() {
            this.template = mustache.render(nav);
        },

        render : function() {
            var that = this;
            console.log(that.collection.fetch());
            this.collection.fetch({
                success : function() {
                    console.log(that.collection.toJSON())
                },

            });

            return;
        },

    });

    return index;

});

来自 fetch (responseText) 的 JSON 响应

[{
    "title": "no titile",
    "sub_title": "no subtitle",
    "content": "no content available",
    "lat": "0",
    "lng": "0",
    "text": "no text"
}, {
    "title": "Example Title",
    "sub_title": "Example Subtitle",
    "content": "Loads on content here",
    "lat": "0",
    "lng": "0",
    "text": "random text bullshit"
}]

【问题讨论】:

  • 您看到的响应格式是什么?可以发一下json吗?您确定 google_places 中的解析功能是必要的吗?响应中真的有 items 数组吗?
  • 我已经在回复中进行了编辑
  • 看起来你不需要 parse 函数,因为响应 json 的顶层已经是一个 google_places 数组。

标签: javascript jquery rest backbone.js requirejs


【解决方案1】:

您的集合parse 方法包含以下内容:

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

但响应没有items 属性:

[{
  "title": "no titile",
  "sub_title": "no subtitle",
  "content": "no content available",
  "lat": "0",
  "lng": "0",
  "text": "no text"
 }, {
  "title": "Example Title",
  "sub_title": "Example Subtitle",
  "content": "Loads on content here",
  "lat": "0",
  "lng": "0",
  "text": "random text bullshit"
}]

因此parse 返回未定义,collection.toJSON() 返回空数组


您应该删除parse 方法,或者您的响应应该是

{
  "items":     [{
    "title": "no titile",
    "sub_title": "no subtitle",
    "content": "no content available",
    "lat": "0",
    "lng": "0",
    "text": "no text"
   }, {
    "title": "Example Title",
    "sub_title": "Example Subtitle",
    "content": "Loads on content here",
    "lat": "0",
    "lng": "0",
    "text": "random text bullshit"
  }]
}

此外,您的收藏似乎没有导入 backbone。确保它可以访问 Backbone 并且没有抛出任何错误

【讨论】:

  • 不是问题的根源,但它帮助我解决了问题,所以谢谢。
【解决方案2】:

删除解析功能。 JSON 已经是一个 google_places 数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多