A Pen by aaron k saunders on CodePen.
Created
January 29, 2015 16:15
-
-
Save aaronksaunders/f2c7e3b98e77dcc7e7e4 to your computer and use it in GitHub Desktop.
Promise sample for Cross-Platform mobile application class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>Simple App - Binding</title> | |
| <!-- see http://getbootstrap.com/css/ --> | |
| <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> | |
| <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script> | |
| </head> | |
| <body ng-app="smallApp" class="container" > | |
| <div ng-controller="MyController"> | |
| {{ progress }} | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Promise sample for Cross-Platform mobile application class | |
| // | |
| // promises as an alternative to callbacks as a primier for | |
| // discussing promise in $http and angular-resource | |
| // create the angular app module | |
| angular.module('smallApp',[]) | |
| // create the angular controller module | |
| .controller( 'MyController', function ($scope, $q) { | |
| $scope.progress = []; | |
| function logInToSystem() { | |
| var deferred = $q.defer(); | |
| deferred.resolve("logged in"); | |
| return deferred.promise; | |
| } | |
| // use the information from previous call ( logInToSystem ) | |
| // to process checkForData, promises return one object which | |
| // will be passed to the next promise/function in the chain | |
| function checkForNewData(_loginInfo) { | |
| var deferred = $q.defer(); | |
| $scope.progress.push(_loginInfo) | |
| deferred.resolve("got the data"); | |
| // if reject, the error will get caught | |
| // in the catch block and processing will | |
| // end - deferred.reject("got the data"); | |
| return deferred.promise; | |
| } | |
| // now that you are loggedin and you have the | |
| // data you can actually display the data | |
| function displayTheData(_newDataInfo) { | |
| var deferred = $q.defer(); | |
| $scope.progress.push(_newDataInfo) | |
| deferred.resolve( "this is the data"); | |
| return deferred.promise; | |
| } | |
| // there are a set of steps that have a dependency on one | |
| // another, we can manage that process through promises | |
| return logInToSystem() | |
| .then(checkForNewData) | |
| .then(displayTheData) | |
| .then(function(_response){ | |
| $scope.progress.push(_response) | |
| }).catch(function(_error){ | |
| alert("Error Processing data " + _error); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment