2

Hi in an answer to the wonderful question Ember.js - CRUD scenarios - Specifying View from within a Route find and findAll is mentioned to be used on the Model to automagically deserialize a URL.

What does this interface look like, and is it really the model and not the controller?

An example would be wonderful.

Community
  • 1
  • 1
Neppord
  • 33
  • 5

1 Answers1

4

As always the documentation is an amazing place to start. Have a look at https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/routable.js#L231-273 for the default serialize/deserialize methods.

You have the right idea though.

  • Ember takes the closest dynamic segment e.g. blog_post_id
  • Strips the _id from the end, leaving blog_post
  • Calls Ember.String.classify('blog_post') which returns BlogPost
  • It looks for that modelClass under the Ember namespace e.g. App.BlogPost
  • It calls find on that model. e.g. App.BlogPost.find(1)

EDIT:

In response to Neppord's comments, he asked about multiple dynamic segments. Yes ember-router does currently support multiple dynamic segments in a single route.

Ember.Route.extend({
  route: "/:post_type/:post_id"
})

If you really need it like that you would have to write your own deserialize/serialize methods. Personally I'd just use a nested dynamic state instead.

Ember.Route.extend({
  route: "/:post_type"
  post: Ember.Route.extend({
    route: "/:post_id"
  })
})
Bradley Priest
  • 7,438
  • 1
  • 29
  • 33
  • Does this mean that you can only use the automated deserialize method if you only have one dynamic segment and only have one field as primary key, and that the name of that field must be id? – Neppord Jul 10 '12 at 11:19
  • Not at all, it takes the last non-matched dynamic segment, so you can happily have multiple. And you can write your find() method to take whatever form you like, so it will happily search by slug etc. – Bradley Priest Jul 10 '12 at 11:30
  • I read in the comments of the code "only guess modelType for states with a single dynamic segment", does this not mean that you cant have urls that are using the standard deserialize method and have multiple segemnts? – Neppord Jul 10 '12 at 11:35