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

Callbacks are wrapped to return values and provide real stacktrace #213

Merged
merged 3 commits into from
May 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions src/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
angular.module('ui.calendar', [])
.constant('uiCalendarConfig', {calendars: {}})
.controller('uiCalendarCtrl', ['$scope',
'$timeout',
'$locale', function(
$scope,
$timeout,
$locale){

var sourceSerialId = 1,
Expand All @@ -23,22 +21,19 @@ angular.module('ui.calendar', [])
extraEventSignature = $scope.calendarWatchEvent ? $scope.calendarWatchEvent : angular.noop,

wrapFunctionWithScopeApply = function(functionToWrap){
var wrapper;

if (functionToWrap){
wrapper = function(){
// This happens outside of angular context so we need to wrap it in a timeout which has an implied apply.
// In this way the function will be safely executed on the next digest.
return function(){
// This may happen outside of angular context, so create one if outside.

if ($scope.$root.$$phase) {
return functionToWrap.apply(this, arguments);
} else {
var args = arguments;
var _this = this;
$timeout(function(){
functionToWrap.apply(_this, args);
var self = this;
return $scope.$root.$apply(function(){
return functionToWrap.apply(self, args);
});
};
}

return wrapper;
}
};
};

this.eventsFingerprint = function(e) {
Expand Down
92 changes: 30 additions & 62 deletions test/calendar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,73 +352,41 @@ describe('uiCalendar', function () {
scope.$apply();
});

it('should make sure that all config functions are called in an angular context', inject(function($timeout, $rootScope){
var functionCount = 0;
scope.uiConfig = {
calendar:{
height: 200,
weekends: false,
defaultView: 'month',
dayClick: function(){},
eventClick: function(){},
eventDrop: function(){},
eventResize: function(){},
eventMouseover: function(){}
}
};
it('should make sure that all config functions are called in an angular context', inject(function($rootScope){
scope.uiConfig = {
calendar:{
height: 200,
weekends: false,
defaultView: 'month'
}
};

spyOn($rootScope,'$apply');
var keys = ['dayClick', 'eventClick', 'eventDrop', 'eventResize', 'eventMouseover'];
angular.forEach(keys, function(key) {
scope.uiConfig.calendar[key] = jasmine.createSpy().andReturn(key);
});

angular.forEach(scope.uiConfig.calendar, function(value,key){
if (typeof value === 'function'){
functionCount++;
spyOn(scope.uiConfig.calendar, key);
var fullCalendarConfig = calendarCtrl.getFullCalendarConfig(scope.uiConfig.calendar, {});

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

fullCalendarConfig[key]();
$timeout.flush();
expect($rootScope.$apply.callCount).toBe(functionCount);
expect(scope.uiConfig.calendar[key]).toHaveBeenCalled();
$rootScope.$apply.isSpy = false;
}
});
}));
angular.forEach(keys, function(key){
$rootScope.$apply.reset();

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

var functionCount = 0;
scope.uiConfig = {
calendar:{
height: 200,
weekends: false,
defaultView: 'month',
dayClick: function(){scope.$apply();},
eventClick: function(){scope.$apply();},
eventDrop: function(){scope.$apply();},
eventResize: function(){scope.$apply();},
eventMouseover: function(){scope.$apply();}
}
};

spyOn($rootScope,'$apply');

angular.forEach(scope.uiConfig.calendar, function(value,key){
if (typeof value === 'function'){
functionCount++;
spyOn(scope.uiConfig.calendar, key);

var fullCalendarConfig = calendarCtrl.getFullCalendarConfig(scope.uiConfig.calendar, {});

fullCalendarConfig[key]();

scope.$apply();
$timeout.flush();
expect($rootScope.$apply.callCount).toBe(functionCount*2);
expect(scope.uiConfig.calendar[key]).toHaveBeenCalled();
}
});
var fn = fullCalendarConfig[key];

expect(fn()).toBe(key);
expect($rootScope.$apply.callCount).toBe(1);
expect(scope.uiConfig.calendar[key].callCount).toBe(1);

expect($rootScope.$apply(function(){
expect($rootScope.$apply.callCount).toBe(2);
return fn();
})).toBe(key);
expect($rootScope.$apply.callCount).toBe(2);
expect(scope.uiConfig.calendar[key].callCount).toBe(2);
});
}));
});

});
});