2

I am trying to call an action in a state directly when I enter it. (Actually in my app I am trying to call an action on my state's parent state, but the net result is the same.)

In my app, I transition from StateOne to StateTwo. When I enter my state, StateTwo, I call manager.send("myStateTwoAction"). However, I immediately get an error

Uncaught Error: <Ember.StateManager:ember1424> 
  could not respond to event myStateTwoAction in state StateOne.

Note that this is in my StateTwo enter method.

Here is my jsFiddle: http://jsfiddle.net/SamFent/YGX2Y/

Does anyone know if there's a way to call a state's action from within the new state's enter method?

Sam Fen
  • 5,074
  • 5
  • 30
  • 56

1 Answers1

2

You cannot call a state action in the enter method. As you can see here: https://github.com/emberjs/ember.js/blob/master/packages/ember-states/lib/state_manager.js#L861 the currentState is set after the enter method is called. But you can implement the setup method witch is called just after a state has been entered.

App.StateTwo = Ember.State.extend({
  enter: function(manager) {
    $("#states").append("<p>State two</p>");
  },
  setup: function(manager){
    manager.send("goToThree");
  },
  goToThree: function(manager) {
    $("#states").append("<p>=> going to three...</p>");
    manager.transitionTo("stateThree");
  }
}),

see: http://jsfiddle.net/YGX2Y/2/

I hope this works for you.

sly7_7
  • 11,961
  • 3
  • 40
  • 54
  • Great, this works fine, thank you. Although can I say that this is really not spelled out in the docs at all (http://docs.emberjs.com/#doc=Ember.StateManager). There is one mention of the "setup" action, which doesn't tell you what it does, and no mention that "enter" is called before the state transition. But what can we do, the Ember docs are way behind -- that's what SO is for. Thanks again! – Sam Fen Sep 12 '12 at 16:54