Ember data: save loses belongsTo relationship

后端 未结 3 1926
北恋
北恋 2021-01-28 15:31

I have the following problem:

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

3条回答
  •  礼貌的吻别
    2021-01-28 16:02

    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,

    Changed the line no 167 to

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

    Update

    Better solution is override serializeHasMany method in your serializer.

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

    Something like

    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.

提交回复
热议问题