Skip to content

Commit a948eb0

Browse files
committed
Remove unnecessary calls to $apply() since $q guarantees .then are run in digest cycles
This simplifies a lot of code
1 parent 0dceef1 commit a948eb0

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

app/scripts/ng-xdLocalStorage.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,8 @@
66

77
angular.module('xdLocalStorage', [])
88
.service('xdLocalStorage', ['$q', '$rootScope', function ($q, $rootScope) {
9-
var iframeReady = false;
109
var apiReady = $q.defer();
1110

12-
var unregister = $rootScope.$watch(function () {
13-
return iframeReady;
14-
}, function () {
15-
if (iframeReady === true) {
16-
apiReady.resolve(true);
17-
unregister();
18-
}
19-
});
20-
2111
function waitForApi() {
2212
if (!xdLocalStorage.wasInit()) {
2313
throw 'You must init xdLocalStorage in app config before use';
@@ -30,9 +20,7 @@ angular.module('xdLocalStorage', [])
3020
var defer = $q.defer();
3121
xdLocalStorage[method].apply(this, args.concat(function () {
3222
var result = arguments[0];
33-
$rootScope.$apply(function () {
34-
defer.resolve(result);
35-
});
23+
defer.resolve(result);
3624
}));
3725
return defer.promise;
3826
});
@@ -41,10 +29,8 @@ angular.module('xdLocalStorage', [])
4129
init: function (options) {
4230
var defer = $q.defer();
4331
options.initCallback = function () {
44-
$rootScope.$apply(function () {
45-
iframeReady = true;
46-
defer.resolve();
47-
});
32+
apiReady.resolve();
33+
defer.resolve();
4834
};
4935
xdLocalStorage.init(options);
5036
return defer.promise;

0 commit comments

Comments
 (0)