2
votes

I'm working with a set of data that can potentially have duplicate values. When I initially add the data I'm using what little information I have available on the client (static info stored on the model in memory).

But because I need to fetch the latest each time the handlebars template is shown I also fire off a "findAll" in the computed property to get any new data that might have hit server side since the initial ember app was launched.

During this process I use the "addObjects" method on the ember-data model but when the server side is returned I see duplicate records in the array (assuming it's because they don't have the same clientId)

App.Day = DS.Model.extend({
    appointments: function() {
        //this will hit a backend server so it's slow
        return App.Appointment.find();
    }.property(),
    slots: function() {
        //no need to hit a backend server here so it's fast
        return App.Slot.all();
    }.property(),
    combined: function() {
        var apts = this.get('apppointments'),
        slots = this.get('slots');
        for(var i = 0; i < slots.get('length'); i++) {
          var slot = slots.objectAt(i);
          var tempApt = App.Appointment.createRecord({start: slot.get('start'), end: slot.get('end')});
          apts.addObjects(tempApt);
        }            

        return apts;
    }.property()
});

Is it possible to tell an ember-data model what makes it unique so that when the promise is resolved it will know "this already exists in the AdapterPopulatedRecordArray so I'll just update it's value instead of showing it twice"

1

1 Answers

2
votes

You can use

DS.RESTAdapter.map('App.Slot', {
  primaryKey: 'name-of-attribute'
});
DS.RESTAdapter.map('App.Appointment', {
  primaryKey: 'name-of-attribute'
});

But I think it is still impossible because App.Slot and App.Appointment are different model classes, so if they have same ids it won't help. You need to use the same model for both slots and appointments for this to work.

Edit

After examinig the source of ember-data, i think that you can define the primaryKey when you define your classes, like:

App.Slot = DS.Model.extend({
    primaryKey: 'myId',
    otherField: DS.attr('number')
});

I didn't tested it though..

Edit 2

After further reading seems that the previous edit is no longer supported. You need to use map as i wrote earlier.