1

I have started working with EmberJS and Ember-Data, but I've run into a small problem.

I have an application where my data source uses a non-id as the primary key (e.g. /model/<slug_name>). I am using the FixtureAdapter to handle things until I connect it to the backend:

App.ApplicationAdapter = DS.FixtureAdapter.extend({})

App.MyModel = DS.Model.extend({
    slug: DS.attr('string'),
    body: DS.attr('string')
});

App.MyModel.FIXTURES = [{
    slug: 'test-slug',
    body: 'Body goes here'
}];

There seem to be several sources of information that explain how to use a different primary key, but they are all oriented around working with the actual data source (and involve modifying the serializer; something that isn't used when working with FixtureAdapter).

If I do the naive thing and just wire up routes...

// ...
App.MyModelEditRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('mymodel', params.slug)
    }
});

... and change the template...

{{#link-to "mymodel.edit" slug}}Edit{{/link-to}}

...then I get this error:

Assertion failed: You made a request for a mymodel with id test-slug, but the adapter's response did not have any data

How can I use a non-'id' field as the primary key when working with a FixtureAdapter?
Ideally, is there a solution that works when using a FixtureAdapter or a real data source?

For reference, these are the versions of EmberJS and Ember-Data that I am using:

"ember": "~1.3.2",
"ember-data": "~1.0.0-beta.8" 
Community
  • 1
  • 1
NT3RP
  • 15,262
  • 9
  • 61
  • 97

1 Answers1

0

I'll have to look back for the fixture adapter/serializer, but in the rest adapter/serializer you can define the primary key on the serializer. Within the app, it'll just be known as id, but when it deserializes/serializes it will transform it to the primary key name

App.ColorSerializer = DS.RESTSerializer.extend({
  primaryKey: 'piano'
});

App.Color = DS.Model.extend({
    color: DS.attr()
});

Example: http://emberjs.jsbin.com/OxIDiVU/774/edit

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96