【问题标题】:What json structure should the ember-data "findMany" have to sideload correctly?ember-data“findMany”必须正确加载什么 json 结构?
【发布时间】:2012-10-01 19:20:18
【问题描述】:

我正在与 ember-data 建立我的第一个 hasMany 关系,并且一直很有趣

“未捕获的错误:断言失败:您的服务器返回了一个带有 键 0 但你没有映射”

这通常意味着我没有我所谓的“ember”友好格式的 json 结构。

我正在使用 django rest 框架为 django 构建我自己的 REST 适配器,所以我很好奇在没有错误的情况下旁加载应该是什么样子。

目前返回的 json 如下所示(显然与它的会话无关,但也许 ember 已经知道如何连接它?)

[{"id": 2, "name": "FooBar"}]

模型看起来像这样

CodeCamp.Session = DS.Model.extend({
    id: DS.attr('number'),
    name: DS.attr('string'),
    room: DS.attr('string'),
    desc: DS.attr('string')
});                 

CodeCamp.Speaker = DS.Model.extend({
    id: DS.attr('number'),
    name: DS.attr('string'),
    session: DS.belongsTo('CodeCamp.Session')
}); 

CodeCamp.Session.reopen({
    speakers: DS.hasMany('CodeCamp.Speaker')
});

提前谢谢你

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    json结构应该是这样的

    { speakers: [{ id: 2, name: "FooBar" }] }
    

    发现这个提交表明我只需要将我的 json 包装在一个命名的字典中

    https://github.com/Kurki/data/commit/f59ad5bc9718634b6f3d59356deae0bf97a1bbd5

    这是我的 django 适配器中的自定义 json 方法

     findMany: function(store, type, ids) {
                var root = this.rootForType(type), plural = this.pluralize(root), json = {};
                this.django_ajax(this.buildURL(root, ids), "GET", {
                    success: function(pre_json) {
                        json[plural] = pre_json;                                                                       
                        this.sideload(store, type, json, plural);
                        store.loadMany(type, json[plural]);
                    }
                });
            }
    

    【讨论】:

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