浏览代码

fix enteredView/leftView

Evan You 12 年之前
父节点
当前提交
f9bebc710b
共有 3 个文件被更改,包括 30 次插入24 次删除
  1. 10 17
      src/transition.js
  2. 1 1
      test/unit/specs/directives.js
  3. 19 6
      test/unit/specs/transition.js

+ 10 - 17
src/transition.js

@@ -21,7 +21,12 @@ var endEvent   = sniffTransitionEndEvent(),
  *    1 = enter
  *    2 = leave
  */
-var transition = module.exports = function (el, stage, changeState, compiler) {
+var transition = module.exports = function (el, stage, cb, compiler) {
+
+    var changeState = function () {
+        cb()
+        compiler.execHook(stage > 0 ? 'enteredView' : 'leftView')
+    }
 
     if (compiler.init) {
         changeState()
@@ -79,7 +84,6 @@ function applyTransitionClass (el, stage, changeState, compiler) {
         classList.add(enterClass)
         // append
         changeState()
-        compiler.execHook('enteredView')
         // force a layout so transition can be triggered
         /* jshint unused: false */
         var forceLayout = el.clientHeight
@@ -98,7 +102,6 @@ function applyTransitionClass (el, stage, changeState, compiler) {
                 // actually remove node here
                 changeState()
                 classList.remove(leaveClass)
-                compiler.execHook('leftView')
             }
         }
         // attach transition end listener
@@ -123,30 +126,20 @@ function applyTransitionFunctions (el, stage, changeState, functionId, compiler)
 
     if (stage > 0) { // enter
         if (typeof enter !== 'function') {
-            doEnter()
+            changeState()
             return codes.JS_SKIP_E
         }
-        enter(el, doEnter)
+        enter(el, changeState)
         return codes.JS_E
     } else { // leave
         if (typeof leave !== 'function') {
-            doLeave()
+            changeState()
             return codes.JS_SKIP_L
         }
-        leave(el, doLeave)
+        leave(el, changeState)
         return codes.JS_L
     }
 
-    function doEnter () {
-        compiler.execHook('enteredView')
-        changeState()
-    }
-
-    function doLeave () {
-        compiler.execHook('leftView')
-        changeState()
-    }
-
 }
 
 /**

+ 1 - 1
test/unit/specs/directives.js

@@ -613,7 +613,7 @@ function mockDirective (dirName, tag, type) {
     var dir = Vue.directive(dirName),
         ret = {
             binding: { compiler: { vm: {} } },
-            compiler: { vm: {}, options: {} },
+            compiler: { vm: {}, options: {}, execHook: function () {}},
             el: document.createElement(tag || 'div')
         }
     if (typeof dir === 'function') {

+ 19 - 6
test/unit/specs/transition.js

@@ -11,16 +11,21 @@ describe('UNIT: Transition', function () {
         
         it('should skip if compiler is in init stage', function () {
             var c = mockChange(),
-                code = transition(null, 1, c.change, { init: true })
+                compiler = mockCompiler()
+            compiler.init = true
+            var code = transition(null, 1, c.change, compiler)
             assert.ok(c.called)
             assert.strictEqual(code, codes.INIT)
+            assert.ok(compiler.enteredView)
         })
 
         it('should skip if no transition is found on the node', function () {
             var c = mockChange(),
-                code = transition(mockEl(), 1, c.change, {})
+                compiler = mockCompiler(),
+                code = transition(mockEl(), 1, c.change, compiler)
             assert.ok(c.called)
             assert.strictEqual(code, codes.SKIP)
+            assert.ok(compiler.enteredView)
         })
 
     })
@@ -31,9 +36,11 @@ describe('UNIT: Transition', function () {
 
             it('should skip if transition is not available', function () {
                 var c = mockChange(),
-                    code = transition(mockEl('css'), 1, c.change, {})
+                    compiler = mockCompiler(),
+                    code = transition(mockEl('css'), 1, c.change, compiler)
                 assert.ok(c.called)
                 assert.strictEqual(code, codes.CSS_SKIP)
+                assert.ok(compiler.enteredView)
             })
 
             // skip the rest
@@ -130,21 +137,27 @@ describe('UNIT: Transition', function () {
 
         it('should skip if correspinding option is not defined', function () {
             var c = mockChange(),
-                code = transition(mockEl('js'), 1, c.change, mockCompiler())
+                compiler = mockCompiler(),
+                code = transition(mockEl('js'), 1, c.change, compiler)
             assert.ok(c.called)
             assert.strictEqual(code, codes.JS_SKIP)
+            assert.ok(compiler.enteredView)
         })
 
         it('should skip if the option is given but the enter/leave func is not defined', function () {
             var c = mockChange(),
-                code = transition(mockEl('js'), 1, c.change, mockCompiler({}))
+                compiler = mockCompiler({}),
+                code = transition(mockEl('js'), 1, c.change, compiler)
             assert.ok(c.called)
             assert.strictEqual(code, codes.JS_SKIP_E)
+            assert.ok(compiler.enteredView)
 
             c = mockChange()
-            code = transition(mockEl('js'), -1, c.change, mockCompiler({}))
+            compiler = mockCompiler({})
+            code = transition(mockEl('js'), -1, c.change, compiler)
             assert.ok(c.called)
             assert.strictEqual(code, codes.JS_SKIP_L)
+            assert.ok(compiler.leftView)
         })
 
         describe('enter', function () {