Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 67c1564

Browse files
RumataEstorjoshkurz
authored andcommitted
Use $scope.$root.$apply instead of $timeout
This also makes possible to return value to fullcalendar.
1 parent f96e7c0 commit 67c1564

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

src/calendar.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,27 @@
1111
angular.module('ui.calendar', [])
1212
.constant('uiCalendarConfig', {calendars: {}})
1313
.controller('uiCalendarCtrl', ['$scope',
14-
'$timeout',
1514
'$locale', function(
1615
$scope,
17-
$timeout,
1816
$locale){
1917

2018
var sources = $scope.eventSources,
2119
extraEventSignature = $scope.calendarWatchEvent ? $scope.calendarWatchEvent : angular.noop,
2220

2321
wrapFunctionWithScopeApply = function(functionToWrap){
24-
var wrapper;
25-
26-
if (functionToWrap){
27-
wrapper = function(){
28-
// This happens outside of angular context so we need to wrap it in a timeout which has an implied apply.
29-
// In this way the function will be safely executed on the next digest.
22+
return function(){
23+
// This may happen outside of angular context, so create one if outside.
3024

25+
if ($scope.$root.$$phase) {
26+
return functionToWrap.apply(this, arguments);
27+
} else {
3128
var args = arguments;
32-
var _this = this;
33-
$timeout(function(){
34-
functionToWrap.apply(_this, args);
29+
var self = this;
30+
return $scope.$root.$apply(function(){
31+
return functionToWrap.apply(self, args);
3532
});
36-
};
37-
}
38-
39-
return wrapper;
33+
}
34+
};
4035
};
4136

4237
var eventSerialId = 1;

test/calendar.spec.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -374,37 +374,40 @@ describe('uiCalendar', function () {
374374
scope.$apply();
375375
});
376376

377-
it('should make sure that all config functions are called in an angular context', inject(function($timeout, $rootScope){
378-
var functionCount = 0;
379-
scope.uiConfig = {
380-
calendar:{
381-
height: 200,
382-
weekends: false,
383-
defaultView: 'month',
384-
dayClick: function(){},
385-
eventClick: function(){},
386-
eventDrop: function(){},
387-
eventResize: function(){},
388-
eventMouseover: function(){}
389-
}
390-
};
377+
it('should make sure that all config functions are called in an angular context', inject(function($rootScope){
378+
scope.uiConfig = {
379+
calendar:{
380+
height: 200,
381+
weekends: false,
382+
defaultView: 'month'
383+
}
384+
};
391385

392-
spyOn($rootScope,'$apply').andCallThrough();
386+
var keys = ['dayClick', 'eventClick', 'eventDrop', 'eventResize', 'eventMouseover'];
387+
angular.forEach(keys, function(key) {
388+
scope.uiConfig.calendar[key] = jasmine.createSpy().andReturn(key);
389+
});
393390

394-
angular.forEach(scope.uiConfig.calendar, function(value,key){
395-
if (typeof value === 'function'){
396-
functionCount++;
397-
spyOn(scope.uiConfig.calendar, key).andCallThrough();
391+
var fullCalendarConfig = calendarCtrl.getFullCalendarConfig(scope.uiConfig.calendar, {});
398392

399-
var fullCalendarConfig = calendarCtrl.getFullCalendarConfig(scope.uiConfig.calendar, {});
393+
spyOn($rootScope,'$apply').andCallThrough();
400394

401-
fullCalendarConfig[key]();
402-
$timeout.flush();
403-
expect($rootScope.$apply.callCount).toBe(functionCount);
404-
expect(scope.uiConfig.calendar[key]).toHaveBeenCalled();
405-
$rootScope.$apply.isSpy = false;
406-
}
407-
});
395+
angular.forEach(keys, function(key){
396+
$rootScope.$apply.reset();
397+
398+
var fn = fullCalendarConfig[key];
399+
400+
expect(fn()).toBe(key);
401+
expect($rootScope.$apply.callCount).toBe(1);
402+
expect(scope.uiConfig.calendar[key].callCount).toBe(1);
403+
404+
expect($rootScope.$apply(function(){
405+
expect($rootScope.$apply.callCount).toBe(2);
406+
return fn();
407+
})).toBe(key);
408+
expect($rootScope.$apply.callCount).toBe(2);
409+
expect(scope.uiConfig.calendar[key].callCount).toBe(2);
410+
});
408411
}));
409412

410413
it('should check that any function that already has an apply in it does not break the calendar (backwards compatible)', inject(function($timeout, $rootScope){

0 commit comments

Comments
 (0)