最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ember: transition to route passing the ID instead of obj - Stack Overflow

programmeradmin3浏览0评论

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

Share Improve this question edited May 18, 2013 at 22:04 Guard asked May 18, 2013 at 17:25 GuardGuard 6,9555 gold badges42 silver badges58 bronze badges 4
  • 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
Add a ment  | 

3 Answers 3

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论