I have some route like /ads/:ad_id and from my controller I can do
this.transitionToRoute('ads.ad', adObj)
How can I do the similar thing but this time passing the ID instead of the loaded object?
O course I understand that I can load an obj by ID first, but Ember's power is in doing lost of boilerplate for us.
Update: So, as by default Ember serializes the model to URL params by doing like
mode_instance -> { model_name_id: model_instance.id }
My trivial attempt was doing
this.transitionToRoute('ads.ad', { id: adObjId })
But when passed a model object Ember does not re-fetch it.
So, the question: I have a route (single ad view) that depends on ad ID. I have this ID as number. I want to transition to this route like if I simply entered the url /ads/ID
I have some route like /ads/:ad_id and from my controller I can do
this.transitionToRoute('ads.ad', adObj)
How can I do the similar thing but this time passing the ID instead of the loaded object?
O course I understand that I can load an obj by ID first, but Ember's power is in doing lost of boilerplate for us.
Update: So, as by default Ember serializes the model to URL params by doing like
mode_instance -> { model_name_id: model_instance.id }
My trivial attempt was doing
this.transitionToRoute('ads.ad', { id: adObjId })
But when passed a model object Ember does not re-fetch it.
So, the question: I have a route (single ad view) that depends on ad ID. I have this ID as number. I want to transition to this route like if I simply entered the url /ads/ID
-
Maybe I'm getting your problem wrong, but what is with
this.transitionToRoute('ads.ad', adObj.get('id'));
? – intuitivepixel Commented May 18, 2013 at 18:04 -
foc ourse I tried it. ember goes to
/ads/undefined
. Probably I can do what I need by overwriting the serialize hook – Guard Commented May 18, 2013 at 18:40 -
yeah, the
serialize
hook is an option I guess – intuitivepixel Commented May 18, 2013 at 19:25 - actually not - see update – Guard Commented May 18, 2013 at 22:03
3 Answers
Reset to default 8This can be acplish by passing the URL to transitionTo. For example,
this.transitionToRoute('/ads/' + adObjId)
The model() method will be called with the params from the URL.
Here is a use case for this:
Transitioning from a list view to a detail view. In the list view, the records don't have any relations tied to them, but the detailed view should side-load relational data. For this reason, the models are not 1:1 between the list view and detailed view. There should be a way to transition simply using the id.
Cp
What's your use case for this? Most cases when you would want to specify an object by id, you already have the object to pass to transitionTo
. Can you provide more context about what you're trying to do? I think you can probably acplish it without using the object id.
In any case, I don't think there's a good way to do this, because when you transition via transitionTo(someRoute, someModel)
, the route's model
hook is not called, and the model you pass in (someModel
) is supplied directly to the other route hooks (setupController(controller, model)
, redirect(model)
, renderTemplate(controller, model)
).
See Ember.JS Route api -- model method for more details.