Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
AngularJS Services
1.
2.
3.
4.
5.
6. $http({ method: 'GET', url: '/someUrl' }).
success(function (data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).
error(function (data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
8. angular.module('MyApp', []).
config(function ($httpProvider) {
// Remove the default AngularJS X-Request-With header
delete $httpProvider.defaults.headers.common['X-Requested-With'];
// Set DO NOT TRACK for all Get requests
$httpProvider.defaults.headers.get['DNT'] = '1';
});
$http.get('api/user', {
// Set the Authorization header. In an actual app, you would
// get the auth token from a service.
headers: {'Authorization': 'Basic Qzsda231231'},
params: {id: 5}
}).success(function() { // Handle success });
9. var module = angular.module('myApp');
module.config(function ($httpProvider) {
$httpProvider.defaults.transformRequest = function (data) {
// We are using jQuery’s param method to convert our
// JSON data into the string form
return $.param(data);
};
});
11. // register the interceptor as a service
$provide.factory('myHttpInterceptor', function ($q, dependency1, dependency2) {
return function (promise) {
return promise.then(function (response) { // do something on success
return response;
},
function (response) {
// do something on error
if (canRecover(response)) { return responseOrNewPromise }
return $q.reject(response);
});
}
});
$httpProvider.responseInterceptors.push('myHttpInterceptor');
// register the interceptor via an anonymous factory
$httpProvider.responseInterceptors.push(function ($q, dependency1, dependency2) {
return function (promise) { // same as above }
});
15. { 'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'} };
var User = $resource('/user/:userId', { userId: '@id' });
var user = User.get({ userId: 123 }, function () {
user.abc = true;
user.$save();
});
16.
17. Kris Kowal's Q
“Interface for interacting with an object that represents the result of an
action that is performed asynchronously, and may or may not be
finished at any given point in time.”
step1(function (val1) {
step2(value1, function (val2) {
step3(value2, function (val3) {
step4(value3, function (val4) {
// Do something with val4
});
});
});
});
Q.fcall(step1)
.then(step2)
.then(step3)
.then(step4)
.then(function (val4) {
// Do something with val4
}, function (error) {
// Handle any error from step1
// through step4
}).done();
18.
19.
20. function asyncGreet(name) {
var deferred = $q.defer();
setTimeout(function () {
scope.$apply(function () {
if (okToGreet(name)) {
deferred.resolve('Hello, ' + name + '!');
} else {
deferred.reject('Greeting'+name+'is not allowed.');
}
});
}, 1000);
return deferred.promise;
}
asyncGreet('Robin Hood').
then( function (greeting) {
alert('Success: ' + greeting);
},
function (reason) {
alert('Failed: ' + reason);
});
21. var myModule = angular.module('myModule', []);
myModule.factory('serviceId', function () {
var shinyNewServiceInstance;
//factory function body that constructs shinyNewServiceInstance
return shinyNewServiceInstance;
});
angular.module('myModule', [], function ($provide) {
$provide.factory('serviceId', function () {
var shinyNewServiceInstance;
//factory function body that constructs shinyNewServiceInstance
return shinyNewServiceInstance;
});
});