Skip to content

Commit a1530b3

Browse files
committed
Fixes for matias's comments
1 parent 2f5e819 commit a1530b3

File tree

9 files changed

+33
-19
lines changed

9 files changed

+33
-19
lines changed

chapter8/datepicker/datepicker.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
angular.module('myApp.directives', []).
2-
directive('datepicker', function() {
1+
angular.module('myApp.directives', [])
2+
.directive('datepicker', function() {
33
return {
4-
restrict: 'A', // Only allow this directive as an attribute
5-
require: '?ngModel', // Always use along with an ng-model
4+
// Enforce the angularJS default of restricting the directive to
5+
// attributes only
6+
restrict: 'A',
7+
// Always use along with an ng-model
8+
require: '?ngModel',
9+
// This method needs to be defined and passed in from the
10+
// passed in to the directive from the view controller
611
scope: {
712
select: '&' // Bind the select function we refer to the right scope
813
},
@@ -14,6 +19,8 @@ angular.module('myApp.directives', []).
1419
optionsObj.dateFormat = 'mm/dd/yy';
1520
var updateModel = function(dateTxt) {
1621
scope.$apply(function () {
22+
// Call the internal AngularJS helper to
23+
// update the two way binding
1724
ngModel.$setViewValue(dateTxt);
1825
});
1926
};
@@ -27,8 +34,8 @@ angular.module('myApp.directives', []).
2734
}
2835
};
2936

30-
//updateModel();
3137
ngModel.$render = function() {
38+
// Use the AngularJS internal 'binding-specific' variable
3239
element.datepicker('setDate', ngModel.$viewValue || '');
3340
};
3441
element.datepicker(optionsObj);

chapter8/file-upload/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<head lang="en">
55
<meta charset="utf-8">
66
<title>File Upload with AngularJS</title>
7+
<!-- Because we are loading jQuery before AngularJS, Angular will use the jQuery library instead of its own jQueryLite implementation -->
78
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
89
<script src="http://raw.github.com/blueimp/jQuery-File-Upload/master/js/vendor/jquery.ui.widget.js"></script>
910
<script src="http://raw.github.com/blueimp/jQuery-File-Upload/master/js/jquery.iframe-transport.js"></script>
@@ -19,6 +20,7 @@
1920

2021
<body ng-controller="MainCtrl">
2122
File Upload:
23+
<!-- We will define uploadFinished in MainCtrl and attach it to the scope, so that it is available here -->
2224
<input id="testUpload" type="file" fileupload name="files[]" data-url="/server/uploadFile" multiple done="uploadFinished(e, data)">
2325
</body>
2426

chapter8/pagination/pagination.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
var app = angular.module('myApp.services', []);
22

33
app.factory('Paginator', function() {
4+
// Despite being a factory, the user of the service gets a new
5+
// Paginator every time he calls the service. This is because
6+
// we return a function that provides an object when executed
47
return function(fetchFunction, pageSize) {
58
pageSize = pageSize || 10;
69
var paginator = {

chapter8/pagination/paginationSpec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ describe('Paginator Service', function() {
55
var paginator;
66

77
var items = [1, 2, 3, 4, 5, 6];
8-
var fetchFn = function(offset, limit, cb) {
9-
cb(items.slice(offset, offset + limit));
8+
var fetchFn = function(offset, limit, callback) {
9+
callback(items.slice(offset, offset + limit));
1010
};
1111

1212
beforeEach(inject(function(Paginator) {

chapter8/server-communication/services.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ servicesModule.factory('Authentication', function() {
4444
};
4545
});
4646

47+
// This factory is only evaluated once, and authHttp is memoized. That is,
48+
// future requests to authHttp service return the same instance of authHttp
4749
servicesModule.factory('authHttp', function($http, Authentication) {
4850
var authHttp = {};
4951

5052
// Append the right header to the request
5153
var extendHeaders = function(config) {
52-
config.headers = config.headers | {};
54+
config.headers = config.headers || {};
5355
config.headers['Authorization'] = Authentication.getTokenType() + ' ' + Authentication.getAccessToken();
5456
};
5557

chapter8/socketio/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var app = angular.module('myApp', []);
22

3+
// We define a factory the socket service is instantiated only once, and
4+
// thus act as a singleton for the scope of the application
35
app.factory('socket', function ($rootScope) {
46
var socket = io.connect('http://localhost:8080');
57
return {

chapter8/socketio/server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var app = require('express')()
1+
var express = require('express')
2+
, app = express()
23
, server = require('http').createServer(app)
34
, io = require('socket.io').listen(server);
45

chapter8/teams-list/index.html

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33

44
<head lang="en">
55
<meta charset="utf-8">
6-
<title>Custom Plunker</title>
6+
<title>Teams List App</title>
77
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
88
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
99
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
1010
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/bootstrap.min.js"></script>
11-
<script>
12-
document.write('<base href="' + document.location + '" />');
13-
</script>
1411
<script src="services.js"></script>
1512
<script src="app.js"></script>
1613
</head>
@@ -30,16 +27,16 @@
3027
<div class="controls-row">
3128
<label for="sportComboBox" class="control-label">Sport:</label>
3229
<div class="controls">
33-
<select id="sportComboBox" ng-model="filterService.filters.sport">
34-
<option ng-repeat="sport in ['Basketball', 'Baseball', 'Football']">{{sport}}</option>
30+
<select id="sportComboBox" ng-model="filterService.activeFilters.sport">
31+
<option ng-repeat="sport in ['Basketball', 'Hockey', 'Football']">{{sport}}</option>
3532
</select>
3633
</div>
3734
</div>
3835

3936
<div class="controls-row">
4037
<label for="cityComboBox" class="control-label">City:</label>
4138
<div class="controls">
42-
<select id="cityComboBox" ng-model="filterService.filters.city">
39+
<select id="cityComboBox" ng-model="filterService.activeFilters.city">
4340
<option ng-repeat="city in ['Dallas', 'Los Angeles', 'Boston', 'New York']">{{city}}</option>
4441
</select>
4542
</div>
@@ -48,7 +45,7 @@
4845
<div class="controls-row">
4946
<label class="control-label">Featured:</label>
5047
<div class="controls">
51-
<input type="checkbox" ng-model="filterService.filters.featured" ng-false-value="" />
48+
<input type="checkbox" ng-model="filterService.activeFilters.featured" ng-false-value="" />
5249
</div>
5350
</div>
5451
</form>
@@ -64,7 +61,7 @@
6461
</tr>
6562
</thead>
6663
<tbody id="teamListTable">
67-
<tr ng-repeat="team in teamsList | filter:filterService.filters | filter:filterService.searchText">
64+
<tr ng-repeat="team in teamsList | filter:filterService.activeFilters | filter:filterService.searchText">
6865
<td>{{team.name}}</td>
6966
<td>{{team.sport}}</td>
7067
<td>{{team.city}}</td>

chapter8/teams-list/services.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
angular.module('myApp.services', []).
22
factory('filterService', function() {
33
return {
4-
filters: {},
4+
activeFilters: {},
55
searchText: ''
66
};
77
});

0 commit comments

Comments
 (0)