diff --git a/demo/js/config/fix.js b/demo/js/config/fix.js index 65a38c144..36cefac0d 100644 --- a/demo/js/config/fix.js +++ b/demo/js/config/fix.js @@ -101,11 +101,16 @@ Demo.FIX_CONFIG = [{ }, { id: 74, pId: 7, - text: "watcher表达式", + text: "watcher且或表达式", value: "demo.fix4" }, { id: 75, pId: 7, + text: "watcher星号表达式", + value: "demo.fix5" +}, { + id: 76, + pId: 7, text: "一个混合的例子", value: "demo.fix" }]; \ No newline at end of file diff --git a/demo/js/fix-2.0/globalwatcher.js b/demo/js/fix-2.0/globalwatcher.js new file mode 100644 index 000000000..7fa8f79fa --- /dev/null +++ b/demo/js/fix-2.0/globalwatcher.js @@ -0,0 +1,49 @@ +;(function () { + var model = Fix.define({ + name: "原始属性", + arr: [{ + n: 'a' + }, { + n: 0 + }] + }); + + Demo.Fix = BI.inherit(BI.Widget, { + _store: function () { + return model; + }, + watch: { + "arr.**": function () { + debugger + }, + "arr.1.*": function () { + this.button.setText(this.model.name + "-" + this.model.arr[1].n) + } + }, + render: function () { + var self = this; + return { + type: "bi.absolute", + items: [{ + el: { + type: "bi.button", + ref: function () { + self.button = this; + }, + handler: function () { + self.model.arr[0].n += 1; + self.model.arr[1].n += 1; + }, + text: this.model.name + "-" + this.model.arr[1].n + } + }] + } + }, + mounted: function () { + + + } + }); + + BI.shortcut("demo.fix5", Demo.Fix); +}()); \ No newline at end of file diff --git a/dist/demo.js b/dist/demo.js index 7fba7c23e..9548abe22 100644 --- a/dist/demo.js +++ b/dist/demo.js @@ -5396,11 +5396,16 @@ Demo.FIX_CONFIG = [{ }, { id: 74, pId: 7, - text: "watcher表达式", + text: "watcher且或表达式", value: "demo.fix4" }, { id: 75, pId: 7, + text: "watcher星号表达式", + value: "demo.fix5" +}, { + id: 76, + pId: 7, text: "一个混合的例子", value: "demo.fix" }];Demo.WIDGET_CONFIG = [{ @@ -10260,6 +10265,54 @@ BI.shortcut("demo.tmp", Demo.Func); }); BI.shortcut("demo.fix", Demo.Fix); +}());;(function () { + var model = Fix.define({ + name: "原始属性", + arr: [{ + n: 'a' + }, { + n: 0 + }] + }); + + Demo.Fix = BI.inherit(BI.Widget, { + _store: function () { + return model; + }, + watch: { + "arr.**": function () { + debugger + }, + "arr.1.*": function () { + this.button.setText(this.model.name + "-" + this.model.arr[1].n) + } + }, + render: function () { + var self = this; + return { + type: "bi.absolute", + items: [{ + el: { + type: "bi.button", + ref: function () { + self.button = this; + }, + handler: function () { + self.model.arr[0].n += 1; + self.model.arr[1].n += 1; + }, + text: this.model.name + "-" + this.model.arr[1].n + } + }] + } + }, + mounted: function () { + + + } + }); + + BI.shortcut("demo.fix5", Demo.Fix); }());;(function(){ var model = Fix.define({ name: "原始属性", diff --git a/dist/fix/fix.js b/dist/fix/fix.js index 2db7fad29..421b549ea 100644 --- a/dist/fix/fix.js +++ b/dist/fix/fix.js @@ -395,12 +395,12 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } Observer.prototype.walk = function walk(obj) { - return defineReactive(obj); + return defineReactive(obj, this); }; Observer.prototype.observeArray = function observeArray(items) { for (var i = 0, l = items.length; i < l; i++) { - items[i] = observe(items[i]).model; + items[i] = observe(items[i], this).model; } return items; }; @@ -422,32 +422,30 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons } } - function observe(value, asRootData) { + function observe(value, parentObserver) { if (!_.isObject(value)) { return; } var ob = void 0; if (_.has(value, '__ob__') && value.__ob__ instanceof Observer) { ob = value.__ob__; - } else if (observerState.shouldConvert && (_.isArray(value) || isPlainObject(value)) && !value._isVue) { + } else if (observerState.shouldConvert && (_.isArray(value) || isPlainObject(value))) { ob = new Observer(value); } - if (asRootData && ob) { - ob.vmCount++; - } + ob.parent = parentObserver || ob.parent; return ob; } function defineReactive(obj, observer, shallow) { - var props = {}; + var model = void 0; _.each(obj, function (val, key) { if (key in $$skipArray) { return; } var dep = observer && observer['__dep' + key] || new Dep(); observer && (observer['__dep' + key] = dep); - var childOb = !shallow && observe(val); + var childOb = !shallow && observe(val, observer); props[key] = { enumerable: true, configurable: true, @@ -470,13 +468,25 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons return; } val = newVal; - childOb = !shallow && observe(newVal); + childOb = !shallow && observe(newVal, observer); obj[key] = childOb ? childOb.model : newVal; dep.notify(); + //触发a.*绑定的hooks + _.each(model.__ob__._deps, function (dep) { + dep.notify(); + }); + //触发a.**绑定的hooks + var parent = model.__ob__; + while (parent) { + _.each(parent._globalDeps, function (dep) { + dep.notify(); + }); + parent = parent.parent; + } } }; }); - return createViewModel$1(obj, props); + return model = createViewModel$1(obj, props); } /** @@ -495,9 +505,6 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons return val; } var ob = target.__ob__; - if (target._isVue || ob && ob.vmCount) { - return val; - } if (!ob) { target[key] = val; return val; @@ -517,9 +524,6 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons return; } var ob = target.__ob__; - if (target._isVue || ob && ob.vmCount) { - return; - } if (!_.has(target, key)) { return; } @@ -951,7 +955,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons options = options || {}; options.user = true; var exps = void 0; - if (_.isFunction(expOrFn) || !(exps = expOrFn.match(/[a-zA-Z0-9_.]+|[|][|]|[&][&]|[(]|[)]/g)) || exps.length === 1) { + if (_.isFunction(expOrFn) || !(exps = expOrFn.match(/[a-zA-Z0-9_.*]+|[|][|]|[&][&]|[(]|[)]/g)) || exps.length === 1 && !/\*/.test(expOrFn)) { var watcher = new Watcher(vm.model, expOrFn, _.bind(cb, vm), options); if (options.immediate) { cb.call(vm, watcher.value); @@ -968,6 +972,32 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons if (_.has(operators, exp)) { return; } + if (/\*\*$|\*$/.test(exp)) { + var isGlobal = /\*\*$/.test(exp); + if (isGlobal) { + //a.**的形式 + exp = exp.replace(".**", ""); + } else { + //a.*的形式 + exp = exp.replace(".*", ""); + } + var getter = parsePath(exp); + var v = getter.call(vm.model, vm.model); + var dep = new Dep(); + if (isGlobal) { + (v.__ob__._globalDeps || (v.__ob__._globalDeps = [])).push(dep); + } else { + (v.__ob__._deps || (v.__ob__._deps = [])).push(dep); + } + var w = new Watcher(vm.model, function () { + dep.depend(); + return NaN; + }, _.bind(cb, vm)); + watchers.push(function unwatchFn() { + w.teardown(); + }); + return; + } var watcher = new Watcher(vm.model, exp, function () { if (complete === true) { return;