Explorar el Código

fix #631 directive two-way set without `update`

Evan You hace 11 años
padre
commit
43fb9c998d
Se han modificado 1 ficheros con 12 adiciones y 11 borrados
  1. 12 11
      src/directive.js

+ 12 - 11
src/directive.js

@@ -62,18 +62,19 @@ p._bind = function (def) {
   if (this.bind) {
   if (this.bind) {
     this.bind()
     this.bind()
   }
   }
-  if (
-    this.update && this._watcherExp &&
-    (!this.isLiteral || this._isDynamicLiteral) &&
-    !this._checkStatement()
-  ) {
+  if (this._watcherExp &&
+      (this.update || this.twoWay) &&
+      (!this.isLiteral || this._isDynamicLiteral) &&
+      !this._checkStatement()) {
     // wrapped updater for context
     // wrapped updater for context
     var dir = this
     var dir = this
-    var update = this._update = function (val, oldVal) {
-      if (!dir._locked) {
-        dir.update(val, oldVal)
-      }
-    }
+    var update = this._update = this.update
+      ? function (val, oldVal) {
+          if (!dir._locked) {
+            dir.update(val, oldVal)
+          }
+        }
+      : function () {} // noop if no update is provided
     // use raw expression as identifier because filters
     // use raw expression as identifier because filters
     // make them different watchers
     // make them different watchers
     var watcher = this.vm._watchers[this.raw]
     var watcher = this.vm._watchers[this.raw]
@@ -97,7 +98,7 @@ p._bind = function (def) {
     this._watcher = watcher
     this._watcher = watcher
     if (this._initValue != null) {
     if (this._initValue != null) {
       watcher.set(this._initValue)
       watcher.set(this._initValue)
-    } else {
+    } else if (this.update) {
       this.update(watcher.value)
       this.update(watcher.value)
     }
     }
   }
   }