3
votes

I'm using ember.js 1.0.0-pre4, ember-data revision 11.

I have the following model:

App.DbProcess = DS.Model.extend({
    pid: DS.attr('number'),
    backendStart: DS.attr('string'),
    transactionStart: DS.attr('string'),
    queryStart: DS.attr('string'),
    stateChange: DS.attr('string'),
    waiting: DS.attr('boolean'),
    state: DS.attr('string'),
    query: DS.attr('string')
})

With the following route:

App.HomeDbProcessesRoute = Ember.Route.extend({
    model: function() {
        return App.DbProcess.find();
    }
})

I then have a template which uses {{#each controller}}{{/each}} to render all the processes retrieved. However if I navigate to other pages (without reloading the page) and returning back to the processes page, the processes will be retrieved again and the duplicates are rendered on page.

EDIT: I also tried this, but it didn't work:

DS.RESTAdapter.map('App.DbProcess', {
    primaryKey: 'pid'
})
2
what does your json look like? does it contain an id property?albertjan
I'm going to assume that you aren't returning a unique IDs in your JSON for the primary key, and so Ember is going to keep appended to the array. This is a requirement, or you can specify a different primary key with primaryKey: on the model. Ember determines unique records by the ID.Wildhoney
Edited, it didn't work.TheOnly92
The primaryKey is not working as intended, you will have to send the id field in your JSON until it is fixed.Shimon Rachlenko
That's unfortunate, I guess that's why ember-data is not 1.0 yet...TheOnly92

2 Answers

2
votes

I had the same issue now and here is my little hot-fix:

{{#if id}}
<div>
    {{title}}
</div>
{{/if}}

In the template I render item from store only if it has id set (only those are coming from databse). But You propably solved it already!

(using revision 12)

0
votes

Turns out you can do something like this to customize the primary key globally

App.Adapter = DS.RESTAdapter.extend({
url: document.location.protocol+'//url-api.com',
serializer: DS.RESTSerializer.extend({
    primaryKey: function(type) {
        // If the type is `BlogPost`, this will return
        // `blog_post_id`.
        var typeString = (''+type).split(".")[1].underscore();
        return typeString + "_id";
    }
})
})