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

Commit bce2616

Browse files
committed
Use $scope.$root.$apply instead of $timeout
This also makes possible to return value to fullcalendar.
1 parent 6e2a52e commit bce2616

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,10 +11,8 @@
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 sourceSerialId = 1,
@@ -23,22 +21,19 @@ angular.module('ui.calendar', [])
2321
extraEventSignature = $scope.calendarWatchEvent ? $scope.calendarWatchEvent : angular.noop,
2422

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

27+
if ($scope.$root.$$phase) {
28+
return functionToWrap.apply(this, arguments);
29+
} else {
3330
var args = arguments;
34-
var _this = this;
35-
$timeout(function(){
36-
functionToWrap.apply(_this, args);
31+
var self = this;
32+
return $scope.$root.$apply(function(){
33+
return functionToWrap.apply(self, args);
3734
});
38-
};
39-
}
40-
41-
return wrapper;
35+
}
36+
};
4237
};
4338

4439
this.eventsFingerprint = function(e) {

test/calendar.spec.js

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

355-
it('should make sure that all config functions are called in an angular context', inject(function($timeout, $rootScope){
356-
var functionCount = 0;
357-
scope.uiConfig = {
358-
calendar:{
359-
height: 200,
360-
weekends: false,
361-
defaultView: 'month',
362-
dayClick: function(){},
363-
eventClick: function(){},
364-
eventDrop: function(){},
365-
eventResize: function(){},
366-
eventMouseover: function(){}
367-
}
368-
};
355+
it('should make sure that all config functions are called in an angular context', inject(function($rootScope){
356+
scope.uiConfig = {
357+
calendar:{
358+
height: 200,
359+
weekends: false,
360+
defaultView: 'month'
361+
}
362+
};
369363

370-
spyOn($rootScope,'$apply').andCallThrough();
364+
var keys = ['dayClick', 'eventClick', 'eventDrop', 'eventResize', 'eventMouseover'];
365+
angular.forEach(keys, function(key) {
366+
scope.uiConfig.calendar[key] = jasmine.createSpy().andReturn(key);
367+
});
371368

372-
angular.forEach(scope.uiConfig.calendar, function(value,key){
373-
if (typeof value === 'function'){
374-
functionCount++;
375-
spyOn(scope.uiConfig.calendar, key).andCallThrough();
369+
var fullCalendarConfig = calendarCtrl.getFullCalendarConfig(scope.uiConfig.calendar, {});
376370

377-
var fullCalendarConfig = calendarCtrl.getFullCalendarConfig(scope.uiConfig.calendar, {});
371+
spyOn($rootScope,'$apply').andCallThrough();
378372

379-
fullCalendarConfig[key]();
380-
$timeout.flush();
381-
expect($rootScope.$apply.callCount).toBe(functionCount);
382-
expect(scope.uiConfig.calendar[key]).toHaveBeenCalled();
383-
$rootScope.$apply.isSpy = false;
384-
}
385-
});
373+
angular.forEach(keys, function(key){
374+
$rootScope.$apply.reset();
375+
376+
var fn = fullCalendarConfig[key];
377+
378+
expect(fn()).toBe(key);
379+
expect($rootScope.$apply.callCount).toBe(1);
380+
expect(scope.uiConfig.calendar[key].callCount).toBe(1);
381+
382+
expect($rootScope.$apply(function(){
383+
expect($rootScope.$apply.callCount).toBe(2);
384+
return fn();
385+
})).toBe(key);
386+
expect($rootScope.$apply.callCount).toBe(2);
387+
expect(scope.uiConfig.calendar[key].callCount).toBe(2);
388+
});
386389
}));
387390
});
388391

0 commit comments

Comments
 (0)