1

I have an ember.js router based application and need to perform some basic logic on an action before transitioning to a different application state. My button's action would look something like

<button {{action doSomethingThenNavigate target="view"}}>Compute the Change</button>

and I would have a doSomethingThenNavigate method on my Ember.View subclass.

My problem is I don't know how to get the router to trigger the change from within the view, I've tried

doSomethingThenNavigate: ->
    console.log "computing something..." 
    App.router.showCat()

where showCat() is a method on my router. This doesn't work.

I have a jsfiddle exemplifying the problem HERE

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
wmarbut
  • 4,595
  • 7
  • 42
  • 72

1 Answers1

2

Here is the working fiddle.

You should have to use:

App.router.send('showCat')

instead of:

App.router.showCat()
wmarbut
  • 4,595
  • 7
  • 42
  • 72
Manoharan
  • 388
  • 1
  • 12
  • 3
    Though that's working, it's a very tight coupling and the event handle of the view would not be easy testable. You shouldn't use `App.router` directly. I'd suggest to use `this.get('controller.target').send('showCat', event);` inside the view's event instead. When using a Router, the `controller.target` is set to the router by default. – pangratz Aug 14 '12 at 05:26
  • @pangratz Any reason to use this.get('controller.target') instead of App.router? Why Ember set the target property in all the controllers during app initialize? – Manoharan Aug 14 '12 at 06:19
  • @ManoHaran Sorry, I thought I made my suggestion clear: when you're using `App.router` directly inside the event handler, you have a tightly coupled code. It may be not that relevant in your code since you always want to send the event to the router. What if you want to reuse your view in another app in another namespace? To be more flexible I would use `controller.target` instead... – pangratz Aug 14 '12 at 06:22
  • @ManoHaran When you're using a router in your app and you're using `App.initialize();` the `target` of the controllers are set to this very router, yes. – pangratz Aug 14 '12 at 06:24