Skip to content

Instantly share code, notes, and snippets.

@partageit
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save partageit/a484c5e10218acc0ccb7 to your computer and use it in GitHub Desktop.
Save partageit/a484c5e10218acc0ccb7 to your computer and use it in GitHub Desktop.
Partage-it.com : transformation de callback en promises
'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