Guard
Guard

Reputation: 6955

Ember: transition to route passing the ID instead of obj

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

Upvotes: 2

Views: 7860

Answers (3)

Scott Nedderman
Scott Nedderman

Reputation: 567

This can be accomplish by passing the URL to transitionTo. For example,

this.transitionToRoute('/ads/' + adObjId)

The model() method will be called with the params from the URL.

Upvotes: 8

cpreid
cpreid

Reputation: 621

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

Upvotes: 7

Sherwin Yu
Sherwin Yu

Reputation: 3230

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 accomplish 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.

Upvotes: 2

Related Questions