forked from shyamseshadri/angularjs-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes to date picker, and add server communication examples
- Loading branch information
1 parent
b411e0f
commit 2efae53
Showing
4 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// <div alert-bar alertMessage="myMessageVar"></div> | ||
angular.module('myApp.directives', []). | ||
directive('alertBar', ['$parse', function($parse) { | ||
return { | ||
restrict: 'A', | ||
template: '<div class="alert alert-error alert-bar" ng-show="errorMessage">' + | ||
'<button type="button" class="close" ng-click="hideAlert()">x</button>' + | ||
'{{errorMessage}}</div>', | ||
|
||
link: function(scope, elem, attrs) { | ||
var alertMessageAttr = attrs['alertmessage']; | ||
scope.errorMessage = null; | ||
|
||
scope.$watch(alertMessageAttr, function(newVal) { | ||
scope.errorMessage = newVal; | ||
}); | ||
scope.hideAlert = function() { | ||
scope.errorMessage = null; | ||
// Also clear the error message on the bound variable | ||
// Do this so that in case the same error happens again | ||
// the alert bar will be shown again next time | ||
$parse(alertMessageAttr).assign(scope, null); | ||
}; | ||
} | ||
}; | ||
}]); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
var servicesModule = angular.module('myApp.services', []); | ||
|
||
servicesModule.factory('errorService', function() { | ||
return { | ||
errorMessage: null, | ||
setError: function(msg) { | ||
this.errorMessage = msg; | ||
}, | ||
clear: function() { | ||
this.errorMessage = null; | ||
} | ||
}; | ||
}); | ||
|
||
servicesModule.config(function ($httpProvider) { | ||
$httpProvider.responseInterceptors.push('errorHttpInterceptor'); | ||
}); | ||
|
||
// register the interceptor as a service, intercepts ALL angular ajax http calls | ||
servicesModule.factory('errorHttpInterceptor', function ($q, $location, ErrorService, $rootScope) { | ||
return function (promise) { | ||
return promise.then(function (response) { | ||
return response; | ||
}, function (response) { | ||
if (response.status === 401) { | ||
$rootScope.$broadcast('event:loginRequired'); | ||
} else if (response.status >= 400 && response.status < 500) { | ||
ErrorService.setError('Server was unable to find what you were looking for... Sorry!!'); | ||
} | ||
return $q.reject(response); | ||
}); | ||
}; | ||
}); | ||
|
||
servicesModule.factory('Authentication', function() { | ||
return { | ||
getTokenType: function() { | ||
return 'Awesome'; | ||
}, | ||
getAccessToken: function() { | ||
// Fetch from the server in real life | ||
return 'asdads131321asdasdasdas'; | ||
} | ||
}; | ||
}); | ||
|
||
servicesModule.factory('authHttp', function($http, Authentication) { | ||
var authHttp = {}; | ||
|
||
// Append the right header to the request | ||
var extendHeaders = function(config) { | ||
config.headers = config.headers | {}; | ||
config.headers['Authorization'] = Authentication.getTokenType() + ' ' + Authentication.getAccessToken(); | ||
}; | ||
|
||
// Do this for each $http call | ||
angular.forEach(['get', 'delete', 'head', 'jsonp'], function(name) { | ||
authHttp[name] = function(url, config) { | ||
config = config || {}; | ||
extendHeaders(config); | ||
return $http[name](url, config); | ||
}; | ||
}); | ||
|
||
angular.forEach(['post', 'put'], function(name) { | ||
authHttp[name] = function(url, data, config) { | ||
config = config || {}; | ||
extendHeaders(config); | ||
return $http[name](url, data, config); | ||
}; | ||
}); | ||
|
||
return authHttp; | ||
}); |