From 53426900abc768356e4c3d470ca4d5dec1a8bf20 Mon Sep 17 00:00:00 2001 From: guy Date: Mon, 30 Oct 2017 16:52:50 +0800 Subject: [PATCH] update --- demo/js/fix-2.0/demo.js | 5 +++- dist/demo.js | 5 +++- dist/fix/fix.js | 53 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/demo/js/fix-2.0/demo.js b/demo/js/fix-2.0/demo.js index fe1d2ce46..f6d30bd71 100644 --- a/demo/js/fix-2.0/demo.js +++ b/demo/js/fix-2.0/demo.js @@ -24,6 +24,9 @@ Demo.Store = BI.inherit(Fix.VM, { computed: { b: function () { return this.comp.c + 1 + }, + c: function () { + return this.comp.name } }, actions: { @@ -39,7 +42,7 @@ Demo.Fix = BI.inherit(BI.Widget, { return new Demo.Store(); }, watch: { - b: function () { + "b&&c||b": function () { debugger; } }, diff --git a/dist/demo.js b/dist/demo.js index 4e6b12974..49edef10a 100644 --- a/dist/demo.js +++ b/dist/demo.js @@ -10096,6 +10096,9 @@ Demo.Store = BI.inherit(Fix.VM, { computed: { b: function () { return this.comp.c + 1 + }, + c: function () { + return this.comp.name } }, actions: { @@ -10111,7 +10114,7 @@ Demo.Fix = BI.inherit(BI.Widget, { return new Demo.Store(); }, watch: { - b: function () { + "b&&c||b": function () { debugger; } }, diff --git a/dist/fix/fix.js b/dist/fix/fix.js index 7fc2bbff3..ee896237f 100644 --- a/dist/fix/fix.js +++ b/dist/fix/fix.js @@ -875,6 +875,18 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons return vm.$watch(keyOrFn, handler, options); } + function runBinaryFunction(binarys) { + var expr = ''; + for (var i = 0, len = binarys.length; i < len; i++) { + if (_.isBoolean(binarys[i]) || binarys[i] === '||' || binarys[i] === '&&' || binarys[i] === '(' || binarys[i] === ')') { + expr += binarys[i]; + } else { + expr += 'false'; + } + } + return new Function('return ' + expr)(); + } + var VM = function () { function VM(model) { _classCallCheck(this, VM); @@ -927,13 +939,42 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } options = options || {}; options.user = true; - var watcher = new Watcher(vm.model, expOrFn, _.bind(cb, vm), options); - if (options.immediate) { - cb.call(vm, watcher.value); + var exps = void 0; + if (_.isFunction(expOrFn) || (exps = expOrFn.match(/[a-zA-Z0-9_.]+|[|][|]|[&][&]|[(]|[)]/g)) && exps.length === 1) { + var watcher = new Watcher(vm.model, expOrFn, _.bind(cb, vm), options); + if (options.immediate) { + cb.call(vm, watcher.value); + } + return function unwatchFn() { + watcher.teardown(); + }; } - return function unwatchFn() { - watcher.teardown(); - }; + var watchers = []; + var fns = exps.slice(); + var complete = false; + _.each(exps, function (exp, i) { + if (exp === '||' || exp === '&&' || exp === '(' || exp === ')') { + return; + } + var watcher = new Watcher(vm.model, exp, function () { + if (complete === true) { + return; + } + fns[i] = true; + if (runBinaryFunction(fns)) { + complete = true; + cb.call(vm); + fns = exps.slice(); + nextTick(function () { + complete = true; + }); + } + }, options); + watchers.push(function unwatchFn() { + watcher.teardown(); + }); + }); + return watchers; }; VM.prototype._init = function _init() {};