Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit cbf7bc0

Browse files
committed
Merge pull request #25 from spiral-project/rewrite_angular_scope_apply
Remove $scope.$apply everywhere
2 parents 3c88f27 + ec33dce commit cbf7bc0

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

examples/angular/app.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ wishlistApp.controller('HomeCtrl', function ($scope, $location, $routeParams, wi
1818

1919
wishlistData
2020
.fetch()
21-
.then(function(response) {
22-
$scope.$apply(function () {
23-
$scope.wishlists = response.records;
24-
});
21+
.thenApply(function(response) {
22+
$scope.wishlists = response.records;
2523
});
2624

2725
$scope.create = function () {
@@ -31,11 +29,9 @@ wishlistApp.controller('HomeCtrl', function ($scope, $location, $routeParams, wi
3129
};
3230
wishlistData
3331
.save(record)
34-
.then(function(response) {
32+
.thenApply(function(response) {
3533
var adminToken = wishlistData.token();
36-
$scope.$apply(function () {
37-
$location.url('/' + response.id + '?token=' + adminToken);
38-
});
34+
$location.url('/' + response.id + '?token=' + adminToken);
3935
});
4036
};
4137
});
@@ -58,17 +54,13 @@ wishlistApp.controller('MainCtrl', function ($scope, $location, $routeParams, wi
5854
wishlistData
5955
.token(adminToken)
6056
.get($routeParams.id)
61-
.then(function(response) {
62-
$scope.$apply(function () {
63-
$scope.master = response;
64-
});
65-
})
66-
.catch(function (e) {
57+
.thenApply(function(response) {
58+
$scope.master = response;
59+
},
60+
function (e) {
6761
// Redirect to Home if wishlist unknown
6862
if (e.status == 404) {
69-
$scope.$apply(function () {
70-
$location.url('/');
71-
});
63+
$location.url('/');
7264
}
7365
});
7466

@@ -77,10 +69,8 @@ wishlistApp.controller('MainCtrl', function ($scope, $location, $routeParams, wi
7769
if (window.confirm("Do you really want to delete this wishlist?")) {
7870
wishlistData
7971
.delete($scope.master.id)
80-
.then(function() {
81-
$scope.$apply(function () {
82-
$location.url('/');
83-
});
72+
.thenApply(function() {
73+
$location.url('/');
8474
});
8575
}
8676
};
@@ -105,21 +95,40 @@ wishlistApp.controller('MainCtrl', function ($scope, $location, $routeParams, wi
10595
var wishlist = angular.copy($scope.master);
10696
wishlistData
10797
.save(wishlist)
108-
.then(function () {
109-
$scope.$apply(function () {
110-
$scope.loading = false;
111-
});
112-
})
113-
.catch(function(error) {
114-
$scope.$apply(function () {
115-
$scope.error = error;
116-
});
98+
.thenApply(function () {
99+
$scope.loading = false;
100+
},
101+
function(error) {
102+
$scope.error = error;
117103
});
118104
}
119105
});
120106

121107

122-
wishlistApp.factory('wishlistData', function() {
108+
wishlistApp.factory('wishlistData', function($rootScope) {
109+
110+
// In this provider, we don't use $http, since we demo the integration
111+
// of daybed.js with Angular.
112+
// Hence, we don't benefit of Angular automatic dirty checking done here:
113+
// https://github.com/angular/angular.js/blob/v1.2.26/src/ng/http.js#L1012-L1013
114+
//
115+
// This piece of code uses ``$rootScope`` from closure, and forces dirty checking
116+
// when the *daybed.js* promises are resolved/rejected.
117+
Promise.prototype.thenApply = function (onFulfilled, onRejected) {
118+
Promise.prototype.then.call(this, applied(onFulfilled), applied(onRejected));
119+
120+
function applied(cb) {
121+
return function () {
122+
if (!cb) return;
123+
var args = arguments;
124+
$rootScope.$apply(function() {
125+
cb.apply(this, args);
126+
});
127+
};
128+
}
129+
};
130+
131+
123132
var server = 'https://daybed.lolnet.org';
124133
var model = 'daybed:examples:wishlist';
125134

0 commit comments

Comments
 (0)