Ember 数据:保存丢失的属于关系 [英] Ember data: save loses belongsTo relationship

查看:22
本文介绍了Ember 数据:保存丢失的属于关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

带有用于选择帖子类别的选择字段的表单.假设帖子的类别为 100.在 Ember 检查器中,如下所示:

A form with a select field for selecting the category of a post. Let's say the post has category 100. In Ember inspector, this is shown as follows:

category: <App.Category:ember708:100>

当我保存帖子时(通过 Ember Data 1.0.0 beta 2),类别突然变为:

When I save the post (via Ember Data 1.0.0 beta 2), the category suddenly changes to:

category: 100

并且在选择列表中不再选择该值.已清除.

And the value is no longer selected in the select list. It is cleared.

要保存的代码:

    post.save().then(
            function () {
              alert("Save OK");
            }
    )

我需要搜索哪个方向的任何想法......如果我转换到另一个页面然后返回到编辑屏幕,值都是正确的.因此模型中的数据仍然是正确的......

Any idea in which direction I need to search ... If I transition to another page and then go back to the edit screen, the values are all correct. The data is thus still correct in the model ...

推荐答案

获取最新的 Canary build,这将解决 belongsTo 问题,但是对于 hasMany 我尝试修改 ember-data 的代码,并且到目前为止它有效,

Get the latest canary build, this will fix the belongsTo issue, but for hasMany I tried modifying the code of ember-data, and it worked so far,

将第 167 行更改为

Changed the line no 167 to

if (relationshipType === 'manyToNone' 
 || relationshipType === 'manyToMany' 
 || relationshipType === 'manyToOne') 

更新

更好的解决方案是覆盖序列化程序中的 serializeHasMany 方法.

Better solution is override serializeHasMany method in your serializer.

感谢 @wycats(根据 github 上的讨论 #1273)

Thanks to @wycats (as per discussion on github #1273)

类似的东西

Deific.AppacitiveRESTSerializer = DS.RESTSerializer.extend({
    //primary key is '__id' in appacitive, overriding default behaviour
    primaryKey: '__id',

    serializeHasMany: function(record, json, relationship) {
        var key = relationship.key;

        var relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship);

        if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany' || relationshipType === 'manyToOne') {
            json[key] = record.get(key).mapBy('id');
        // TODO support for polymorphic manyToNone and manyToMany relationships
        }
    }
});

Deific.Store = DS.Store.extend({
    revision: 12,
    adapter: DS.RESTAdapter.extend({
        namespace: 'service',
        defaultSerializer: 'Deific/appacitiveREST'
    }),
});

暂时可以使用这个.希望这会有所帮助.

For time being this can be used. Hope this helps.

这篇关于Ember 数据:保存丢失的属于关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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