Преглед изворни кода

force deferreds to resolve synchronously, fixes jQuery 3 promise compatibility. closes #3197

Adam Shaw пре 9 година
родитељ
комит
02b7b50da5
2 измењених фајлова са 21 додато и 8 уклоњено
  1. 7 8
      src/common/View.js
  2. 14 0
      src/util.js

+ 7 - 8
src/common/View.js

@@ -281,15 +281,14 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 
 		this.calendar.freezeContentHeight();
 
-		return this.clear().then(function() { // clear the content first (async)
+		return syncThen(this.clear(), function() { // clear the content first
 			return (
 				_this.displaying =
-					$.when(_this.displayView(date)) // displayView might return a promise
-						.then(function() {
-							_this.forceScroll(_this.computeInitialScroll(scrollState));
-							_this.calendar.unfreezeContentHeight();
-							_this.triggerRender();
-						})
+					syncThen(_this.displayView(date), function() { // displayView might return a promise
+						_this.forceScroll(_this.computeInitialScroll(scrollState));
+						_this.calendar.unfreezeContentHeight();
+						_this.triggerRender();
+					})
 			);
 		});
 	},
@@ -303,7 +302,7 @@ var View = FC.View = Class.extend(EmitterMixin, ListenerMixin, {
 		var displaying = this.displaying;
 
 		if (displaying) { // previously displayed, or in the process of being displayed?
-			return displaying.then(function() { // wait for the display to finish
+			return syncThen(displaying, function() { // wait for the display to finish
 				_this.displaying = null;
 				_this.clearEvents();
 				return _this.clearView(); // might return a promise. chain it

+ 14 - 0
src/util.js

@@ -929,3 +929,17 @@ function debounce(func, wait, immediate) {
 		return result;
 	};
 }
+
+
+// HACK around jQuery's now A+ promises: execute callback synchronously if already resolved.
+// thenFunc shouldn't accept args.
+// similar to whenResources in Scheduler plugin.
+function syncThen(promise, thenFunc) {
+	// not a promise, or an already-resolved promise?
+	if (!promise || !promise.then || promise.state() === 'resolved') {
+		return $.when(thenFunc()); // resolve immediately
+	}
+	else if (thenFunc) {
+		return promise.then(thenFunc);
+	}
+}