Implementing a State Machine in Angular.js to control routing

Posted by ldn_tech_exec on Stack Overflow See other posts from Stack Overflow or by ldn_tech_exec
Published on 2012-11-22T04:56:52Z Indexed on 2012/11/22 4:59 UTC
Read the original article Hit count: 327

Can anyone help me with integrating a state machine to control routing?

What's the best method to do this? Create a service?

I need to basically intercept every $location request, run the state machine and let it figure out what the next $location.path should be.

Think of the problem like a bank of questions that get added and removed over time. The user visits once in a while, passes in the user's answers object to the statemachine, and the statemachine figures out which question to load. This is my pseudocode, but i need to figure out where to put this or what event I can hook into to make sure all route requests are passed through the machine. Do I need a specific stateMachine controller? Do I create a service? Where do I use the service? Do I need to override $locationProvider?

$scope.user.answers = [{

id: 32,
answer: "whatever"

},
{

id:33,
answer: "another answer"

}]

$scope.questions = [{

id:32, 
question:"what is your name?", 
path:"/question/1"

},{

id:34, 
question:"how old are you?", 
path:"/question/2"

}]

var questions = $scope.questions;

angular.forEach(questions, function(question) {

if(question.id !exist in $scope.user.answers.id) {

$location.path = question.path
break;

});

Thanks

© Stack Overflow or respective owner

Related posts about angularjs

Related posts about statemachine