2

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?
patmood
  • 41
  • 1
  • 4
  • possible duplicate of [ember.js struggling with transitionTo from view or controller](http://stackoverflow.com/questions/13484238/ember-js-struggling-with-transitionto-from-view-or-controller) – givanse May 14 '14 at 06:42

1 Answers1

5

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 i.e. the controller or even better to the route, inside a specific action i.e. 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 i.e. inside viewthis.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/)

melc
  • 11,523
  • 3
  • 36
  • 41
  • If you create the application using ember-cli, through the view, you need to do this.get('controller').transitionToRoute(..) would work – inquisitive May 05 '15 at 04:59