Duplicating nested models on ember.js (after save)


#1
App.Campaign = DS.Model.extend({
    name: DS.attr('string'),
    stories: DS.hasMany('story'),
    domains: DS.hasMany('domain')
});

App.Domain = DS.Model.extend({
    domain: DS.attr('string'),
    campaigns: DS.hasMany('campaign')
});

App.Story = DS.Model.extend({
    title: DS.attr('string'),
    campaign: DS.belongsTo('campaign')
});

In controller I create currentCampaign in this way:

this.currentCampaign = this.store.createRecord(‘campaign’);

And add stories and domains in this way:

var campaignDomains = this.get('currentCampaign.domains');
var domainObj = this.store.createRecord('domain', {
    "domain": domain
});
campaignDomains.addObject(domainObj);

var campaignStories = this.get('currentCampaign.stories');
var story = this.store.createRecord('story', {
    "id": this.generateUUID(), // Generate uniqid
    "title": storyTitle,
});
campaignStories.pushObject(story);

After saving (before calling transitionTo method I see duplicating domains and stories on template). When I go to page editing campaign I also see duplicating domains and stories. AFter refresh page - I see normal data (by refresh this data are fetching from server).

POST request JSON (domain id are generating on server side, story id are generating on client side):

{"campaign":
    {
    "id":"f0777f1a17deadcb",
    "name":"name of campaign",
    "stories":[{
        "id":"488b6b6bf4c988f0",
        "title":"story",
        "campaign":"f0777f1a17deadcb"
    }],
    "domains":[{
        "domain":"domain.com",
        "campaigns":["f0777f1a17deadcb"]}
    }] }

Response on POST request (by adding new campaign):

{"campaign":
    {
    "id":"f0777f1a17deadcb",
    "name":"name of campaign",
    "stories":[{
         "id":"488b6b6bf4c988f0",
         "title":"story",
         "campaign":"f0777f1a17deadcb"
    }],
    "domains":[{
         "id":"54",
         "domain":"domain.com",
         "campaigns":["f0777f1a17deadcb"]}
    }]
}

Serializer file

App.CampaignSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        domains: {embedded: 'always'},
        stories: {embedded: 'always'}
    }
});

Ember 1.8.1 Ember-data 1.0.0-beta.14.1


#2

I know, long time no answer, but well if its not too late:

Update to Beta 16 or as temporary hack/fix remove those records from the store.

someRecord.save().then(function(record{
    this.store.all('someRecord').filterBy('id', null).invoke('deleteRecord');
}))

*EDIT: forgot the most important part with the filterBy


#3

Here is an issue tracking this problem: https://github.com/emberjs/data/issues/1829