angular promise - AngularJS - series of user prompts that store value in an object -


i have angular app interface resembles "command line", user can type in commands navigate site. when user types "sign-up", ask user series of questions (ex. name, email, etc), , each question, type in command line submit data. seems simple enough, can't figure out best way execute (haha, it? execute? okay...)

anyway here's pseudo-code explains need happen:

html:

<div id="cli">     <p ng-repeat="message in output">{{message}}</p>      <form ng-submit="execute()">         <input type="text" ng-model="input">     </form> </div> 

js:

app.controller("commandctrl", function($scope) {

    var output = {},      prompts = [         {             message: "what's name?",             responsetype: "string",             value: "name"         },         {             message: "what's email?",             responsetype: "email",             value: "email"         },         {             message: "are sure want continue? (y/n)",             responsetype: "y/n",             value: null         }     ],      programs = {         signup: function() {             // correct way loop through these prompts,             // showing 1 @ time , waiting response?              foreach(prompts, function(promptdata) {                 prompt(promptdata.message)                  .then(function(input) {                     if (validate(input, promptdata.value)) {                         output[promptdata.message] = input;                          gotonextarrayitem();                     } else {                         // input invalid, tell user , ask data again                         tryitagain();                     }                 });             });         }     }      $scope.execute = function() {         var input = $scope.input;          if (input == 'sign-up') {             programs.signup();         }          $scope.$broadcast("recievedinput", input);     }      function prompt(message) {         return $q(function(resolve, reject) {             writeline(message);              $scope.$on("recievedinput", function(input) {                 resolve(message);             });         });     }      function writeline(message) {         $scope.output.push(message);     }      function validate(input, datatype) {         switch (datatype) {             case: 'string':                 return typeof input == 'string';                 break;              case 'email':                 // hypothetical email validation function                 return validateemail(datatype);                 break;              ....              default:                 .....         }     } }); 

basically need loop through prompts, run prompt() function each 1 (which should return promise when user enters something). need able take input user each prompt , save in output object, able "continue" next prompt or "go back" previous one. there way loop through promises in angular?


Comments