简体   繁体   中英

Ember.js Transition to route after handling View event

Setup:

I've got an Ember app that supports image uploading using the Imgur API.

I already have a working Route and Template to handle any Imgur ID, but I want to transition to this route after a new image is uploaded (using the Imgur ID that is returned)

Here is the relevant section of the app: http://jsbin.com/iGOXivEW/2/edit

Questions:

  1. Is this proper use of a View and the correct way to handle the change event?
  2. How can I transition to the 'imgur' route from the View and pass it the new Imgur ID?

You handle the ui event inside view which is fine. You could possibly separate the logic of sending to igur or somewhere else, from the view logic, by moving the code of posting the image to the controller.

The transition can be accomplished by calling from view,

this.controller.transitionToRoute('imgur', imgurId);

or if you move everything to the controller,

transitionToRoute('imgur', imgurId);

( http://emberjs.com/api/classes/Ember.Controller.html#method_transitionToRoute )

Design wise, regardless of where the logic of posting the image goes, you may find helpful if you move the transition logic to a common place ie the controller or even better to the route, inside a specific action ie completed.

This way you always now where to maintain logic that handles navigation from the current route. To call an action in controller or route from view or controller you need to use function send ie inside view this.controller.send('completed') , inside controller this.send('completed'); . The actions need to be placed within an action object.

( http://emberjs.com/guides/views/handling-events/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM