فهرست منبع

use "enter/leaveCancelled" hooks instead of returning it from enter/leave

Evan You 11 سال پیش
والد
کامیت
f8e98ceacf
2فایلهای تغییر یافته به همراه16 افزوده شده و 12 حذف شده
  1. 9 6
      src/transition/transition.js
  2. 7 6
      test/unit/specs/transition/transition_spec.js

+ 9 - 6
src/transition/transition.js

@@ -29,7 +29,7 @@ function Transition (el, id, hooks, vm) {
   // async state
   this.pendingCssEvent =
   this.pendingCssCb =
-  this.jsCancel =
+  this.cancel =
   this.pendingJsCb =
   this.op =
   this.cb = null
@@ -76,6 +76,7 @@ p.enter = function (op, cb) {
   addClass(this.el, this.enterClass)
   op()
   this.callHookWithCb('enter')
+  this.cancel = this.hooks && this.hooks.enterCancelled
   queue.push(this.enterNextTick)
 }
 
@@ -104,7 +105,7 @@ p.enterNextTick = function () {
  */
 
 p.enterDone = function () {
-  this.jsCancel = this.pendingJsCb = null
+  this.cancel = this.pendingJsCb = null
   removeClass(this.el, this.enterClass)
   this.callHook('afterEnter')
   if (this.cb) this.cb()
@@ -138,6 +139,7 @@ p.leave = function (op, cb) {
   this.cb = cb
   addClass(this.el, this.leaveClass)
   this.callHookWithCb('leave')
+  this.cancel = this.hooks && this.hooks.enterCancelled
   // only need to do leaveNextTick if there's no explicit
   // js callback
   if (!this.pendingJsCb) {
@@ -166,6 +168,7 @@ p.leaveNextTick = function () {
  */
 
 p.leaveDone = function () {
+  this.cancel = this.pendingJsCb = null
   this.op()
   removeClass(this.el, this.leaveClass)
   this.callHook('afterLeave')
@@ -194,9 +197,9 @@ p.cancelPending = function () {
     removeClass(this.el, this.enterClass)
     removeClass(this.el, this.leaveClass)
   }
-  if (this.jsCancel) {
-    this.jsCancel.call(null)
-    this.jsCancel = null
+  if (this.cancel) {
+    this.cancel.call(this.vm, this.el)
+    this.cancel = null
   }
 }
 
@@ -229,7 +232,7 @@ p.callHookWithCb = function (type) {
     if (hook.length > 1) {
       this.pendingJsCb = _.cancellable(this[type + 'Done'])
     }
-    this.jsCancel = hook.call(this.vm, this.el, this.pendingJsCb)
+    hook.call(this.vm, this.el, this.pendingJsCb)
   }
 }
 

+ 7 - 6
test/unit/specs/transition/transition_spec.js

@@ -456,15 +456,16 @@ if (_.inBrowser && !_.isIE9) {
         })
       })
 
-      it('optional cleanup callback', function (done) {
+      it('cancel hook', function (done) {
         var cleanupSpy = jasmine.createSpy('js cleanup')
         var leaveSpy = jasmine.createSpy('js leave')
+        var timeout
         hooks.enter = function (el, done) {
-          var to = setTimeout(done, 30)
-          return function () {
-            clearTimeout(to)
-            cleanupSpy()
-          }
+          timeout = setTimeout(done, 30)
+        }
+        hooks.enterCancelled = function () {
+          clearTimeout(timeout)
+          cleanupSpy()
         }
         hooks.leave = function (el, done) {
           expect(cleanupSpy).toHaveBeenCalled()