Skip to content

Commit 2efae53

Browse files
committed
Fixes to date picker, and add server communication examples
1 parent b411e0f commit 2efae53

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

chapter8/datepicker/datepicker.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ angular.module('myApp.directives', []).
33
return {
44
restrict: 'A', // Only allow this directive as an attribute
55
require: '?ngModel', // Always use along with an ng-model
6+
scope: {
7+
select: '&' // Bind the select function we refer to the right scope
8+
},
69
link: function(scope, element, attrs, ngModel) {
710
if (!ngModel) return;
811

@@ -17,9 +20,9 @@ angular.module('myApp.directives', []).
1720

1821
optionsObj.onSelect = function(dateTxt, picker) {
1922
updateModel(dateTxt);
20-
if (attrs.onSelect) {
23+
if (scope.select) {
2124
scope.$apply(function() {
22-
scope[attrs.onSelect](dateTxt);
25+
scope.select({date: dateTxt});
2326
});
2427
}
2528
};

chapter8/datepicker/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</head>
1818

1919
<body ng-controller="MainCtrl">
20-
<input id="dateField" datepicker ng-model="currentDate" on-select="updateMyText(date)">
20+
<input id="dateField" datepicker ng-model="$parent.currentDate" select="updateMyText(date)">
2121
<br/>
2222
{{myText}} - {{currentDate}}
2323
</body>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// <div alert-bar alertMessage="myMessageVar"></div>
2+
angular.module('myApp.directives', []).
3+
directive('alertBar', ['$parse', function($parse) {
4+
return {
5+
restrict: 'A',
6+
template: '<div class="alert alert-error alert-bar" ng-show="errorMessage">' +
7+
'<button type="button" class="close" ng-click="hideAlert()">x</button>' +
8+
'{{errorMessage}}</div>',
9+
10+
link: function(scope, elem, attrs) {
11+
var alertMessageAttr = attrs['alertmessage'];
12+
scope.errorMessage = null;
13+
14+
scope.$watch(alertMessageAttr, function(newVal) {
15+
scope.errorMessage = newVal;
16+
});
17+
scope.hideAlert = function() {
18+
scope.errorMessage = null;
19+
// Also clear the error message on the bound variable
20+
// Do this so that in case the same error happens again
21+
// the alert bar will be shown again next time
22+
$parse(alertMessageAttr).assign(scope, null);
23+
};
24+
}
25+
};
26+
}]);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var servicesModule = angular.module('myApp.services', []);
2+
3+
servicesModule.factory('errorService', function() {
4+
return {
5+
errorMessage: null,
6+
setError: function(msg) {
7+
this.errorMessage = msg;
8+
},
9+
clear: function() {
10+
this.errorMessage = null;
11+
}
12+
};
13+
});
14+
15+
servicesModule.config(function ($httpProvider) {
16+
$httpProvider.responseInterceptors.push('errorHttpInterceptor');
17+
});
18+
19+
// register the interceptor as a service, intercepts ALL angular ajax http calls
20+
servicesModule.factory('errorHttpInterceptor', function ($q, $location, ErrorService, $rootScope) {
21+
return function (promise) {
22+
return promise.then(function (response) {
23+
return response;
24+
}, function (response) {
25+
if (response.status === 401) {
26+
$rootScope.$broadcast('event:loginRequired');
27+
} else if (response.status >= 400 && response.status < 500) {
28+
ErrorService.setError('Server was unable to find what you were looking for... Sorry!!');
29+
}
30+
return $q.reject(response);
31+
});
32+
};
33+
});
34+
35+
servicesModule.factory('Authentication', function() {
36+
return {
37+
getTokenType: function() {
38+
return 'Awesome';
39+
},
40+
getAccessToken: function() {
41+
// Fetch from the server in real life
42+
return 'asdads131321asdasdasdas';
43+
}
44+
};
45+
});
46+
47+
servicesModule.factory('authHttp', function($http, Authentication) {
48+
var authHttp = {};
49+
50+
// Append the right header to the request
51+
var extendHeaders = function(config) {
52+
config.headers = config.headers | {};
53+
config.headers['Authorization'] = Authentication.getTokenType() + ' ' + Authentication.getAccessToken();
54+
};
55+
56+
// Do this for each $http call
57+
angular.forEach(['get', 'delete', 'head', 'jsonp'], function(name) {
58+
authHttp[name] = function(url, config) {
59+
config = config || {};
60+
extendHeaders(config);
61+
return $http[name](url, config);
62+
};
63+
});
64+
65+
angular.forEach(['post', 'put'], function(name) {
66+
authHttp[name] = function(url, data, config) {
67+
config = config || {};
68+
extendHeaders(config);
69+
return $http[name](url, data, config);
70+
};
71+
});
72+
73+
return authHttp;
74+
});

0 commit comments

Comments
 (0)