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

Commit 3fc96af

Browse files
author
Costin Zaharia
committed
Wrapped the events (dayClick,, eventClick, eventDrop and eventResize) with $timeout in order to run them in angular context. Scope.apply will not be needed anymore in client code. Added a new command "refetchEvents".
1 parent fa98004 commit 3fc96af

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

demo/calendarDemo.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,15 @@ function CalendarCtrl($scope) {
4545
};
4646
/* alert on eventClick */
4747
$scope.alertEventOnClick = function( date, allDay, jsEvent, view ){
48-
$scope.$apply(function(){
49-
$scope.alertMessage = ('Day Clicked ' + date);
50-
});
48+
$scope.alertMessage = ('Day Clicked ' + date);
5149
};
5250
/* alert on Drop */
5351
$scope.alertOnDrop = function(event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view){
54-
$scope.$apply(function(){
55-
$scope.alertMessage = ('Event Droped to make dayDelta ' + dayDelta);
56-
});
52+
$scope.alertMessage = ('Event Droped to make dayDelta ' + dayDelta);
5753
};
5854
/* alert on Resize */
5955
$scope.alertOnResize = function(event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view ){
60-
$scope.$apply(function(){
61-
$scope.alertMessage = ('Event Resized to make dayDelta ' + minuteDelta);
62-
});
56+
$scope.alertMessage = ('Event Resized to make dayDelta ' + minuteDelta);
6357
};
6458
/* add and removes an event source of choice */
6559
$scope.addRemoveEventSource = function(sources,source) {

demo/index.html

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,15 @@ <h3>How?</h3>
225225
};
226226
/* alert on eventClick */
227227
$scope.alertEventOnClick = function( date, allDay, jsEvent, view ){
228-
$scope.$apply(function(){
229-
$scope.alertMessage = ('Day Clicked ' + date);
230-
});
228+
$scope.alertMessage = ('Day Clicked ' + date);
231229
};
232230
/* alert on Drop */
233231
$scope.alertOnDrop = function(event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view){
234-
$scope.$apply(function(){
235-
$scope.alertMessage = ('Event Droped to make dayDelta ' + dayDelta);
236-
});
232+
$scope.alertMessage = ('Event Droped to make dayDelta ' + dayDelta);
237233
};
238234
/* alert on Resize */
239235
$scope.alertOnResize = function(event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view ){
240-
$scope.$apply(function(){
241-
$scope.alertMessage = ('Event Resized to make dayDelta ' + minuteDelta);
242-
});
236+
$scope.alertMessage = ('Event Resized to make dayDelta ' + minuteDelta);
243237
};
244238
/* add and removes an event source of choice */
245239
$scope.addRemoveEventSource = function(sources,source) {

src/calendar.js

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,40 @@
1010

1111
angular.module('ui.calendar', [])
1212
.constant('uiCalendarConfig', {})
13-
.controller('uiCalendarCtrl', ['$scope', function($scope){
14-
15-
var sourceSerialId = 1,
13+
.controller('uiCalendarCtrl', ['$scope', '$timeout', function($scope, $timeout){
14+
15+
var sourceSerialId = 1,
1616
eventSerialId = 1,
17-
sources = $scope.eventSources;
18-
extraEventSignature = $scope.calendarWatchEvent ? $scope.calendarWatchEvent : angular.noop;
17+
sources = $scope.eventSources,
18+
extraEventSignature = $scope.calendarWatchEvent ? $scope.calendarWatchEvent : angular.noop,
19+
20+
wrapFunctionWithScopeApply = function(functionToWrap){
21+
var wrapper;
22+
23+
if (functionToWrap){
24+
wrapper = function(){
25+
// This happens outside of angular context so we need to wrap it in a timeout which has an implied apply.
26+
// In this way the function will be safely executed on the next digest.
27+
28+
var args = arguments;
29+
$timeout(function(){
30+
functionToWrap.apply(this, args);
31+
});
32+
};
33+
}
34+
35+
return wrapper;
36+
},
37+
38+
addRefetchEventsCommand = function (settings, calendar) {
39+
if (!settings) {
40+
return;
41+
}
42+
43+
settings.refetchEvents = function () {
44+
calendar.fullCalendar('refetchEvents');
45+
};
46+
};
1947

2048
this.eventsFingerprint = function(e) {
2149
if (!e.__uiCalId) {
@@ -132,7 +160,23 @@ angular.module('ui.calendar', [])
132160
};
133161
};
134162

163+
this.addCommands = function (settings, calendar) {
164+
addRefetchEventsCommand(settings, calendar);
165+
};
166+
167+
this.getFullCalendarConfig = function(calendarSettings, uiCalendarConfig){
168+
var config = {};
135169

170+
angular.extend(config, uiCalendarConfig);
171+
angular.extend(config, calendarSettings);
172+
173+
config.dayClick = wrapFunctionWithScopeApply(config.dayClick);
174+
config.eventClick = wrapFunctionWithScopeApply(config.eventClick);
175+
config.eventDrop = wrapFunctionWithScopeApply(config.eventDrop);
176+
config.eventResize = wrapFunctionWithScopeApply(config.eventResize);
177+
178+
return config;
179+
};
136180
}])
137181
.directive('uiCalendar', ['uiCalendarConfig', '$locale', function(uiCalendarConfig, $locale) {
138182
// Configure to use locale names by default
@@ -160,14 +204,22 @@ angular.module('ui.calendar', [])
160204
link: function(scope, elm, attrs, controller) {
161205

162206
var sources = scope.eventSources,
163-
sourcesChanged = false;
207+
sourcesChanged = false,
164208
eventSourcesWatcher = controller.changeWatcher(sources, controller.sourcesFingerprint),
165209
eventsWatcher = controller.changeWatcher(controller.allEvents, controller.eventsFingerprint),
166210
options = null;
167-
211+
168212
function getOptions(){
213+
var calendarSettings = attrs.uiCalendar ? scope.$parent.$eval(attrs.uiCalendar) : {},
214+
fullCalendarConfig;
215+
216+
controller.addCommands(calendarSettings, scope.calendar);
217+
218+
fullCalendarConfig = controller.getFullCalendarConfig(calendarSettings, uiCalendarConfig);
219+
169220
options = { eventSources: sources };
170-
angular.extend(options, uiCalendarConfig, attrs.uiCalendar ? scope.$parent.$eval(attrs.uiCalendar) : {});
221+
angular.extend(options, fullCalendarConfig);
222+
171223
var options2 = {};
172224
for(var o in options){
173225
if(o !== 'eventSources'){
@@ -184,7 +236,7 @@ angular.module('ui.calendar', [])
184236
scope.calendar = elm.html('');
185237
}
186238
};
187-
239+
188240
scope.init = function(){
189241
scope.calendar.fullCalendar(options);
190242
};
@@ -233,5 +285,4 @@ angular.module('ui.calendar', [])
233285
});
234286
}
235287
};
236-
}]);
237-
288+
}]);

0 commit comments

Comments
 (0)