0

I'm very new to Ember.

Im having trouble with this dynamic url feature in nested routes.

I understand nesting routes in general like:

App.Router.map(function(), {
  this.resource('orders', function(){
    this.route('order', { path: "/orders/:order_id" })
  })
})

I'm know to put {{ outlet }} inside orders template to display the nested order object.

But my question is, how does the naming convention of :order_id work in relation to my model data?

If my orders data is :

{
  id: 1,
  name: "John Doe",
  address: "123 example rd",
  telephone: "5145555555"
}

Does this :order_id work similar to rails in that it prepends the object name ":order_" to the JSON attribute of "id"?

Im pretty sure that's the case, but im not sure what else I'm missing to display a specific object when i visit the path: "/orders/1"

Probably an Ember.ObjectController somewhere, but I cant quite figure it out.

2 Answers2

0

The order_id needs to be a property of the object that will be used in transitions and linkTo.

{{linkTo 'orders.order' obj}}

In the case above for your route obj.object_id should resolve. So you have two options:

(1) Set the dynamic segment to :id

path: "/orders/:id"

Or (2) add an order_id to your model. If you are using ember-data, you can easily create an aliased property:

order_id: Ember.computed.alias('id'),
Amir T
  • 2,708
  • 18
  • 21
0

Your dynamic slug should either match the identifier on the record, or you should override the serializer on the route so as to provide the necessary information to ember for when it attempts to generate your url.

http://emberjs.jsbin.com/AvOYIwE/2/edit

App.OrderRoute = Ember.Route.extend({
  model: function(params) {
   this.get('store').find('order', params.order_id);
  },
  serialize: function(model){
   //if your model doesn't have a property that matches the slug you must override the serialize method
   return {order_id: model.get('id')}; 
  }
});
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96