Last active
August 29, 2015 14:06
-
-
Save partageit/a484c5e10218acc0ccb7 to your computer and use it in GitHub Desktop.
Partage-it.com : transformation de callback en promises
This file contains 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
'use strict'; | |
angular.module('promiseTestApp') | |
.controller('LoginWithCallbacksCtrl', function ($scope, Connection) { | |
$scope.connect = function(login, password) { | |
$scope.showLoader(); | |
Connection.get( | |
{'login': login, 'password': password}, | |
function() { // success | |
$scope.notification('Connection successful'); | |
$scope.hideLoader(); | |
}, | |
function(response) { // failure | |
$scope.notificationError('Connection failed: ' + response.data.reason); | |
$scope.hideLoader(); | |
} | |
); | |
}; | |
}); | |
angular.module('promiseTestApp') | |
.controller('LoginWithPromisesCtrl', function ($scope, Connection) { | |
$scope.connect = function(login, password) { | |
$scope.showLoader(); | |
Connection.get({'login': login, 'password': password}).$promise | |
.then(function() { // success | |
$scope.notification('Connection successful'); | |
}) | |
.catch(function(response) { // failure | |
$scope.notificationError('Connection failed: ' + response.data.reason); | |
}) | |
['finally'](function() { | |
$scope.hideLoader(); | |
}); | |
}; | |
}); | |
angular.module('promiseTestApp') | |
.controller('LoginWithChainedPromisesCtrl', function ($scope, Connection, UserData) { | |
$scope.connect = function(login, password) { | |
$scope.showLoader(); | |
Connection.get({'login': login, 'password': password}).$promise | |
.then(function(result) { // connection success | |
$scope.notification('Connection successful'); | |
$scope.userData = UserData.get({'token': result.token}); | |
return $scope.userData.$promise; | |
}) | |
.then(function(result) { // user data success | |
$scope.notification('Welcome ' + result.nickName); | |
}) | |
.catch(function(response) { // failure | |
$scope.notificationError('Connection failed: ' + response.data.reason); | |
}) | |
['finally'](function() { | |
$scope.hideLoader(); | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment