Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BelongsTo seems not update canonicalRecord when it is set to null #3640

Closed
SelokYeung opened this issue Aug 12, 2015 · 3 comments
Closed

BelongsTo seems not update canonicalRecord when it is set to null #3640

SelokYeung opened this issue Aug 12, 2015 · 3 comments

Comments

@SelokYeung
Copy link

I was trying to rollback a relationship, and I found that canonicalRecord didn't update after save if it's value is set to null. I don't know whether it is issue, but it is weird for canonicalRecord still keep the invalid record after save.

data/packages/ember-data/lib/system/relationships/state/belongs-to.js

I guess it should be modify as follow

BelongsToRelationship.prototype.setCanonicalRecord = function (newRecord) {
    if (newRecord) {
        this.addCanonicalRecord(newRecord);
    } else {
        this.removeCanonicalRecord(this.canonicalState);
        if (this.inverseRecord) {
            this.removeCanonicalRecord(this.inverseRecord);
        }
    }
    this.setHasData(true);
};
@gilpinheiro
Copy link

I'm seeing the same thing, this has bitten me. ember-data: 1.13.4

@SelokYeung
Copy link
Author

I do it in dirty ways, by adding a hook to didUpdate event.
Everytime those model saved, it will check all belongsto relationship. If it is empty then cleanup canonical record.

onDataChanged: function () {
    var self = this;

    this.eachRelationship(function (name, relationship) {
        if (relationship.kind === 'belongsTo') {
            if (Em.isEmpty(self.get(name))) {
                var internalRelation = self.get('_internalModel')._relationships.get(name);
                internalRelation.removeCanonicalRecord(internalRelation.canonicalState);
            }
        }
    });
}.on('didUpdate'),

Seems work fine in ember-data: 1.13 and 2.0

@bmac
Copy link
Member

bmac commented Mar 27, 2016

Resolved via 8400b9d

@bmac bmac closed this as completed Mar 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants