将外键保存到具有hasMany关系的其他Model [英] Save foreign key to other Model with hasMany relation

查看:189
本文介绍了将外键保存到具有hasMany关系的其他Model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题。

在我的应用程序中,我有一个屏幕来创建一个新的站点。但是,当我通过控制器上的操作保存新站点时,不会使用语言 --property发送

In my app I have a screen to make a new Site. But when I save the new site via an action on the controller, the languages-property isn't sent with the POST-request to the server.

添加新网站的模板是这样的:

The template for adding a new Site is this:

<form class="form-horizontal">
<div class="control-group">
    <label class="control-label" for="name">Name</label>
    <div class="controls">
        {{view Ember.TextField valueBinding="name"}}
    </div>
</div>
<div class="control-group">
    <label class="control-label" for="languages">Languages</label>
    <div class="controls">
        {{view Ember.Select contentBinding="controllers.languages" selectionBinding="languages" optionValuePath="content.id" optionLabelPath="content.description" multiple="true"}}
            </div>
</div>
<div class="form-actions">
    <button {{ action "createSite" }} class="btn btn-primary">Save</button>
</div>

我定义了我的存储如下:

App.Store = DS.Store.extend({
    revision : 12,
    adapter : DS.RESTAdapter.extend({
        namespace : 'rest'
    })
});

这是我的控制器:

App.SitesNewController = Em.ObjectController.extend({
    needs: ['languages'],
    name: null,
    languages: null,
    createSite : function() {
        var self = this;
        var name = this.get('name');
        var languages = this.get('languages');
        // Create the new Site model
        var s = App.Site.createRecord({
            name : name
        });
        $.each(languages,function(i,lang) {
            s.get('languages').addObject(lang);
        });

        this.get('store').commit();
    }
});

这是网站 -model



This is the Site-model

App.Site = DS.Model.extend({
    name : DS.attr('string'),
    languages : DS.hasMany('App.Language')
});

语言 -model:

App.Language = DS.Model.extend({
    description : DS.attr('string')
});

发送给我的 POST 服务器是这样的:

The POST-request data sent to my server is this:

{
  "site":{"name":"test"}
}

所以我想念 code> - 属性。实际上,我希望一个 language_ids 属性与一个数组的数组。

So I miss the language-property. Actually I expect a language_ids property with an array of id's.

当我编辑我的 RESTAdapter 配置如下:

When I edit my RESTAdapter-configuration like this:

DS.RESTAdapter.map('App.Site', {
    languages: { embedded: 'always' }
});

现在, POST -request数据是:

Now the POST-request data is:

{
  "site": {
    "name":"test",
    "languages":[{
      "id":2,"description":"English"
    },{
      "id":3,"description":"Deutsch"
    }]
  }
}

已知语言嵌入在请求数据中。这是没有问题的,在后端我得到 id 之前,我保存它。但是,它知道它希望将语言 -data嵌入到GET-响应中。

The languages are know embedded in the request-data. This is no problem, at the backend I get the id before I save it. But know it expects the language-data to be embedded in the GET-responses also.

在POST数据中只发送 id 的方法是什么?我想要这样做:

What is the way to send just the id's in the POST-data? I want it to be something like this:

{
  "site": {
    "name":"test",
    "languages":[2,3]
  }
}


推荐答案

这个答案主要来源于这个其他StackOverflow的答案

App.Store = DS.Store.extend({
    revision : 12,
    adapter : DS.RESTAdapter.extend({
        namespace : 'rest',
        serializer: DS.RESTSerializer.extend({
            addHasMany: function (hash, record, key, relationship) {
                var type = record.constructor,
                    name = relationship.key,
                    serializedHasMany = [], 
                    manyArray, embeddedType;

                embeddedType = this.embeddedType(type, name);
                if (embeddedType !== 'always') { return; }

                manyArray = record.get(name);

                manyArray.forEach(function (record) {
                    serializedHasMany.push(record.get('id'));
                }, this);

                hash[this.singularize(key) + '_ids'] = serializedHasMany;
            }

        })

    })
});

为了参考,您可以查看 JSONSerializer ,其中 RESTSerializer 大部分继承自。可以找到 addHasMany 的代码 here

For reference, you might review the JSONSerializer, which the RESTSerializer mostly inherits from. The code for what addHasMany does can be found here.

请注意,对于上述代码段,唯一的行真正不同的是最后几个。而不是序列化嵌入式记录,ids被推送到奇异值下的哈希(我将使用 RESTSerializer#keyForHasMany 如果它不是自己检查嵌入式类型

Note that, for the above snippet, the only lines that really differ are the last several. Rather than serializing embedded records, ids are pushed to the hash under the singularized key (I would have used RESTSerializer#keyForHasMany if it didn't have its own check for embedded types.

这篇关于将外键保存到具有hasMany关系的其他Model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆