Skip to content

Commit 63d4f14

Browse files
author
lavinjj
committed
Cleaned up code to follow AngularJS Best Practices. Added Pub/Sub Channel for alternate solution
1 parent 76f82d2 commit 63d4f14

File tree

396 files changed

+66406
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

396 files changed

+66406
-1
lines changed

ReadMe.md

+2-1

app/app/app.js sample01/app/app.js

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

app/index.html sample01/index.html

File renamed without changes.

sample02/app/app.js

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
'use strict';
2+
3+
// Declare app level module which depends on filters, and services
4+
var app = angular.module('myApp', ['mongolabResourceHttp', 'data.services']);
5+
6+
app.config(['$httpProvider', function ($httpProvider) {
7+
var $http,
8+
interceptor = ['$q', '$injector', function ($q, $injector) {
9+
var notificationChannel;
10+
11+
function success(response) {
12+
// get $http via $injector because of circular dependency problem
13+
$http = $http || $injector.get('$http');
14+
// don't send notification until all requests are complete
15+
if ($http.pendingRequests.length < 1) {
16+
// get requestNotificationChannel via $injector because of circular dependency problem
17+
notificationChannel = notificationChannel || $injector.get('requestNotificationChannel');
18+
// send a notification requests are complete
19+
notificationChannel.requestEnded();
20+
}
21+
return response;
22+
}
23+
24+
function error(response) {
25+
// get $http via $injector because of circular dependency problem
26+
$http = $http || $injector.get('$http');
27+
// don't send notification until all requests are complete
28+
if ($http.pendingRequests.length < 1) {
29+
// get requestNotificationChannel via $injector because of circular dependency problem
30+
notificationChannel = notificationChannel || $injector.get('requestNotificationChannel');
31+
// send a notification requests are complete
32+
notificationChannel.requestEnded();
33+
}
34+
return $q.reject(response);
35+
}
36+
37+
return function (promise) {
38+
// get requestNotificationChannel via $injector because of circular dependency problem
39+
notificationChannel = notificationChannel || $injector.get('requestNotificationChannel');
40+
// send a notification requests are complete
41+
notificationChannel.requestStarted();
42+
return promise.then(success, error);
43+
}
44+
}];
45+
46+
$httpProvider.responseInterceptors.push(interceptor);
47+
}]);
48+
49+
app.factory('requestNotificationChannel', ['$rootScope', function($rootScope){
50+
// private notification messages
51+
var _START_REQUEST_ = '_START_REQUEST_';
52+
var _END_REQUEST_ = '_END_REQUEST_';
53+
54+
// publish start request notification
55+
var requestStarted = function() {
56+
$rootScope.$broadcast(_START_REQUEST_);
57+
};
58+
// publish end request notification
59+
var requestEnded = function() {
60+
$rootScope.$broadcast(_END_REQUEST_);
61+
};
62+
// subscribe to start request notification
63+
var onRequestStarted = function($scope, handler){
64+
$scope.$on(_START_REQUEST_, function(event){
65+
handler();
66+
});
67+
};
68+
// subscribe to end request notification
69+
var onRequestEnded = function($scope, handler){
70+
$scope.$on(_END_REQUEST_, function(event){
71+
handler();
72+
});
73+
};
74+
75+
return {
76+
requestStarted: requestStarted,
77+
requestEnded: requestEnded,
78+
onRequestStarted: onRequestStarted,
79+
onRequestEnded: onRequestEnded
80+
};
81+
82+
83+
84+
}]);
85+
86+
app.directive('loadingWidget', ['requestNotificationChannel', function (requestNotificationChannel) {
87+
return {
88+
restrict: "A",
89+
link: function (scope, element) {
90+
// hide the element initially
91+
element.hide();
92+
93+
var startRequestHandler = function() {
94+
// got the request start notification, show the element
95+
element.show();
96+
};
97+
98+
var endRequestHandler = function() {
99+
// got the request start notification, show the element
100+
element.hide();
101+
};
102+
103+
requestNotificationChannel.onRequestStarted(scope, startRequestHandler);
104+
105+
requestNotificationChannel.onRequestEnded(scope, endRequestHandler);
106+
}
107+
};
108+
}]);
109+
110+
app.filter('startFrom', function () {
111+
return function (input, start) {
112+
start = +start; //parse to int
113+
return input.slice(start);
114+
}
115+
});
116+
117+
app.controller('myController', ['$scope', 'YeastResource', function ($scope, YeastResource) {
118+
$scope.yeasts = [];
119+
$scope.currentPage = 0;
120+
$scope.pageSize = 10;
121+
122+
$scope.numberOfPages = function () {
123+
return Math.ceil($scope.yeasts.length / $scope.pageSize);
124+
};
125+
126+
$scope.init = function () {
127+
YeastResource.query({}, {sort: {Type: 1, Name: 1}}).then(function (yeast) {
128+
$scope.yeasts = yeast;
129+
});
130+
};
131+
132+
$scope.init();
133+
}]);

sample02/app/data-services.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
/* Services */
4+
5+
// Demonstrate how to register services
6+
// In this case it is a simple value service.
7+
angular.module('data.services', []).
8+
constant('MONGOLAB_CONFIG',{API_KEY:'50a70bf4e4b039251d28b93b', DB_NAME:'breweverywhere_backup'}).
9+
factory('AdjunctResource',function ($mongolabResourceHttp) {
10+
return $mongolabResourceHttp('adjuncts');
11+
}).
12+
factory('BrewerResource',function ($mongolabResourceHttp) {
13+
return $mongolabResourceHttp('brewers');
14+
}).
15+
factory('EquipmentResource',function ($mongolabResourceHttp) {
16+
return $mongolabResourceHttp('equipment');
17+
}).
18+
factory('FermentableResource',function ($mongolabResourceHttp) {
19+
return $mongolabResourceHttp('fermentables');
20+
}).
21+
factory('HopResource',function ($mongolabResourceHttp) {
22+
return $mongolabResourceHttp('hops');
23+
}).
24+
factory('MashProfileResource',function ($mongolabResourceHttp) {
25+
return $mongolabResourceHttp('mashprofiles');
26+
}).
27+
factory('RecipeResource',function ($mongolabResourceHttp) {
28+
return $mongolabResourceHttp('recipes');
29+
}).
30+
factory('StyleResource',function ($mongolabResourceHttp) {
31+
return $mongolabResourceHttp('styles');
32+
}).
33+
factory('WaterProfileResource',function ($mongolabResourceHttp) {
34+
return $mongolabResourceHttp('waterprofiles');
35+
}).
36+
factory('YeastResource', function ($mongolabResourceHttp) {
37+
return $mongolabResourceHttp('yeast');
38+
});

0 commit comments

Comments
 (0)