Explorar el Código

simplify value change check for v-model modifiers

Evan You hace 9 años
padre
commit
29d6e33b33

+ 11 - 11
src/platforms/web/runtime/directives/model.js

@@ -40,17 +40,17 @@ export default {
       if (isIE || isEdge) {
         setTimeout(cb, 0)
       }
-    } else if (
-      (vnode.tag === 'textarea' || el.type === 'text') &&
-      !binding.modifiers.lazy
-    ) {
-      if (!isAndroid) {
-        el.addEventListener('compositionstart', onCompositionStart)
-        el.addEventListener('compositionend', onCompositionEnd)
-      }
-      /* istanbul ignore if */
-      if (isIE9) {
-        el.vmodel = true
+    } else if (vnode.tag === 'textarea' || el.type === 'text') {
+      el._vModifiers = binding.modifiers
+      if (!binding.modifiers.lazy) {
+        if (!isAndroid) {
+          el.addEventListener('compositionstart', onCompositionStart)
+          el.addEventListener('compositionend', onCompositionEnd)
+        }
+        /* istanbul ignore if */
+        if (isIE9) {
+          el.vmodel = true
+        }
       }
     }
   },

+ 3 - 14
src/platforms/web/runtime/modules/dom-props.js

@@ -36,8 +36,8 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
       // avoid resetting cursor position when value is the same
       const strCur = cur == null ? '' : String(cur)
       if (!elm.composing && (
-          (document.activeElement !== elm && elm.value !== strCur) ||
-          isValueChanged(vnode, strCur)
+        (document.activeElement !== elm && elm.value !== strCur) ||
+        isValueChanged(vnode, strCur)
       )) {
         elm.value = strCur
       }
@@ -49,7 +49,7 @@ function updateDOMProps (oldVnode: VNodeWithData, vnode: VNodeWithData) {
 
 function isValueChanged (vnode: VNodeWithData, newVal: string): boolean {
   const value = vnode.elm.value
-  const modifiers = getModelModifier(vnode)
+  const modifiers = vnode.elm._vModifiers // injected by v-model runtime
   if ((modifiers && modifiers.number) || vnode.elm.type === 'number') {
     return toNumber(value) !== toNumber(newVal)
   }
@@ -59,17 +59,6 @@ function isValueChanged (vnode: VNodeWithData, newVal: string): boolean {
   return value !== newVal
 }
 
-function getModelModifier (vnode: VNodeWithData): ?ASTModifiers {
-  const directives = vnode.data.directives
-  if (!directives) return
-  for (let i = 0, directive; i < directives.length; i++) {
-    directive = directives[i]
-    if (directive.name === 'model') {
-      return directive.modifiers
-    }
-  }
-}
-
 export default {
   create: updateDOMProps,
   update: updateDOMProps