Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top