简体   繁体   English

带有嵌入式记录的持久灰烬模型

[英]Persist ember model with embedded record

I have the following ember models: 我有以下余烬模型:

App.Location = DS.Model.extend({
    street: DS.attr('string'),
    city: DS.attr('string'),
    area: DS.attr('string'),
    lat: DS.attr('string'),
    lng: DS.attr('string'),
    report: DS.belongsTo('report', {embedded: 'always'})
});

App.Report = DS.Model.extend({
    title: DS.attr('string'),
    description: DS.attr('string'),
    reg_num: DS.attr('string'),
    location_str: DS.attr('string'),
    location: DS.belongsTo('location', {embedded: 'always'})
});

In App.ReportController when I try to save the report I also want to embed the location object in the request payload. App.ReportController当我尝试保存报表时,我也想将位置对象嵌入请求有效负载中。 So far I tried the following code: 到目前为止,我尝试了以下代码:

App.ReportController = Ember.ObjectController.extend({
    actions: {
        saveReport: function(record) {
            var self = this,
                report = this.get('model'),
                location = this.store.createRecord('location', {
                               lat: this.get('lat'),
                               lng: this.get('lng')
                           });
                report.set('location', location);
                report.save().then(function (result) {
                    self.transitionToRoute('list');
                });
            }
        }
    }
});

However in the request payload location is always location: null . 但是,在请求中,有效负载位置始终为location: null

How to add location to the request payload? 如何在请求有效负载中添加location

Maybe I'am missing something, but I know two different ways of handling embedded records (ember-data >= 1.0.0-beta): 也许我缺少了一些东西,但是我知道两种处理嵌入式记录的方式(ember-data> = 1.0.0-beta):

  1. Rewriting DS.Adapter's methods : serialize/extract*/normalize and others, - to set JSON payload in a way which Ember can work with. 重写DS.Adapter的方法 :serialize / extract * / normalize和其他方法-以Ember可以使用的方式设置JSON有效负载。
  2. Using DS.EmbeddedRecordsMixin for configuring DS.Serializer. 使用DS.EmbeddedRecordsMixin配置DS.Serializer。

I can assume you get location: null because of default DS.Serializer's behavior with serializing relationships : Ember expects id value in belongTo property and list of ids in hasMany property. 我可以假设您获得了location: null由于默认的DS.Serializer的行为与序列化关系而为location: null :Ember期望id值在belongTo属性和id的列表中hasMany属性。 LocationModel doesn't have an id after creating and before saving into server database. 在创建之后以及保存到服务器数据库之前, LocationModel没有ID。 When default serializer tries to form an outgoing json payload while saving ReportModel with attached newly created LocationModel , it gets id of LocationModel ( null ) and puts that id in location property. 当默认序列化程序尝试在保存ReportModel和新创建的LocationModel同时形成传出的json负载时,它将获取LocationModel id( null )并将该id放入location属性。

Putting composite JavaScript objects in the payload shouldn't be rocket science, I hope there is a less complicated way to achieve this. 将复合JavaScript对象放入有效负载中不应该是火箭科学,我希望有一种不太复杂的方法来实现这一目标。

Make sure that you don't have belongsTo defined for both relations because you will end up with stack overflow: 确保没有为两个关系都定义belongsTo ,因为这样会导致堆栈溢出:

App.Location = DS.Model.extend({
    street: DS.attr('string'),
    city: DS.attr('string'),
    area: DS.attr('string'),
    lat: DS.attr('string'),
    lng: DS.attr('string')
});

App.Report = DS.Model.extend({
    title: DS.attr('string'),
    description: DS.attr('string'),
    reg_num: DS.attr('string'),
    location_str: DS.attr('string'),
    location: DS.belongsTo('location', {embedded: 'always'})
});

I had to configure the PostSerializer and override the serializeBelongsTo to make this work 我必须配置PostSerializer并重写serializeBelongsTo才能使此工作

App.PostSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        location: {embedded: 'always'},
        report: {embedded: 'always'}
    }
});

DS.JSONSerializer.reopen({
    serializeBelongsTo: function(record, json, relationship) {
        var key = relationship.key,
            belongsToRecord = Ember.get(record, key);

        if (relationship.options.embedded === 'always') {
            json[key] = belongsToRecord.serialize();
        } else {
            return this._super(record, json, relationship);
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM