Ember Data 1.13.x + Async Everything + Slugs Dillema

Hey Crew!

Working on a new app that is JSONAPI driven. Every relationship is Async. From what I’m led to believe, this is the happiest of happy paths to loving Ember Data.

However, I’m not necessarily happy right now. I’m puzzled. Some people like being puzzled. I guess I like it, but not when I have a deadline!

OK so here’s my initial problem:

Let’s say I have a data structure like this

country → hasMany → states

 # think rails style fixtures   
  id: 1

  id: 1
  slug: 'cali'
  id: 2
  slug: 'ny'

Then I go and render a countries.show route where on the template, there is a big old list of states, and links to them.

How can I can these links look like /cali and /ny without loading the entire set of states for the usa model? The country only has access to the id of each of it’s related model at the point of the index.

Core Problem: To handle the above problem, I’m currently using the slug as the frontend id, so to speak. This works great! Great, unless you need to update the thing that makes the slug.

Let’s say I:

  • Change the name of a State
  • Friendly ID is going to go and regenerate the slug
  • The server passes back the payload with a different ID (remember I’m using the model’s slug as ID?)
  • Ember Data says:

Assertion Failed: An adapter cannot assign a new id to a record that already has an id. <admin@state::ember1447:cali> had id: cali and you tried to update it with california. This likely happened because your server returned data in response to a find or update that had a different id than the one you sent.

Thoughts? I’m using the latest Active Model Serializers with the JSONAPI adapter. There’s probably a nice way to do this with model fragments or something!

1 Like