javascript – States Machine with AngularJS

Question:

I'm having trouble implementing a state machine using AngularJS…

On my first try, I tried using this one made in JS: https://github.com/jakesgordon/javascript-state-machine

I couldn't make it work as I wanted. I followed the examples and got no result. What I tried was to create some simple steps using only console.log() to see where it went, and I ended up noticing that it doesn't go through all the states I defined, it just runs the first state:

angular.module('app').controller('ConfigurationCtrl', function($scope, $http){

$scope.welcome = "Welcome to the jungle"
$scope.percentBar = 0;

var fsm = StateMachine.create({

  events: [
    { name: 'play', from: 'none', to: 'game' },
    { name: 'quit', from: 'game', to: 'teste' },
    { name: 'stop', from: 'teste', to: 'game' }
  ],

  callbacks: {

    onentermenu: function() { console.log("onentermenu"); },
    onentergame: function() { console.log("onentergame"); },
    onenterteste: function() {console.log("onenterteste")},

    onleavemenu: function() {
        trace("onleavemenu");
      $http.get("http://192.168.11.51:8080/sigetall").sucess(function (data) {
        console.log(data);
        fsm.transition();   
      });         
      return StateMachine.ASYNC; 
    },

    onleavegame: function() {
        console.log("fuck")
      $http.get("http://192.168.11.51:8080/sigetall").sucess(function (data) {
        console.log(data);
        fsm.transition();   
      });         
      return StateMachine.ASYNC; 
    },

    onleaveteste: function() {

    }

  }
});

console.log(fsm.current)
fsm.play();
console.log(fsm.current)
});

result:

none
ConfigurationCtrl.js:17 onentergame
ConfigurationCtrl.js:47 game

I ended up changing the onentermenu method and leaving it the same as onleavemenu to see how it would work assync and it returns an error saying that the fms.transition() method does n't exist.

Can someone help me?

Answer:

Change:

fsm.transition();

For:

this.transition();
Scroll to Top