Question

In my application I want route to resource "day" to looke like this "sampledomain.com/day/1-3-2014".

I defined it like this:

this.resource('day', { path: '/day/:day_date' });

My model hook for that route looks like this:

model: function(params) {
    return this.store.find('day', params.day_date);
},

And my API response looks like this:

{"day":{"id":"3","dayDate":"2014-03-01","openTime":null,"closeTime":null}}

For some reason I'm getting two records into the store. One is correct and one with id set like the dynamic part from URL (1-3-2014) and the rest of this model data as empty.

I have no idea what I'm doing wrong.

Was it helpful?

Solution 2

You're pretending the date is the id, so Ember Data is building up a record based on the id 1-3-2014 then your server is returning a record with an id of 3. So that begs the question, which is the id?

If dayDate can be the id, the primaryKey for the record should change, if it can't, then really you should be using findQuery (or find with an object).

return this.store.find('day', {dayDate: params.day_date});

This will change your result set to a collection, since you aren't going by id, it's no longer guaranteed to be a single instance.

Additionally the get request will be different

OTHER TIPS

You should use a findQuery and return the first record inside the promise resolve handler:

App.DayRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('day', { date: params.day_date }).then(function(days) {
      return days.toArray()[0];
    });
  }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top