guy 7 years ago
parent
commit
8f006451e2
  1. 7
      demo/js/config/fix.js
  2. 49
      demo/js/fix-2.0/globalwatcher.js
  3. 55
      dist/demo.js
  4. 66
      dist/fix/fix.js

7
demo/js/config/fix.js

@ -101,11 +101,16 @@ Demo.FIX_CONFIG = [{
}, { }, {
id: 74, id: 74,
pId: 7, pId: 7,
text: "watcher表达式", text: "watcher且或表达式",
value: "demo.fix4" value: "demo.fix4"
}, { }, {
id: 75, id: 75,
pId: 7, pId: 7,
text: "watcher星号表达式",
value: "demo.fix5"
}, {
id: 76,
pId: 7,
text: "一个混合的例子", text: "一个混合的例子",
value: "demo.fix" value: "demo.fix"
}]; }];

49
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);
}());

55
dist/demo.js vendored

@ -5396,11 +5396,16 @@ Demo.FIX_CONFIG = [{
}, { }, {
id: 74, id: 74,
pId: 7, pId: 7,
text: "watcher表达式", text: "watcher且或表达式",
value: "demo.fix4" value: "demo.fix4"
}, { }, {
id: 75, id: 75,
pId: 7, pId: 7,
text: "watcher星号表达式",
value: "demo.fix5"
}, {
id: 76,
pId: 7,
text: "一个混合的例子", text: "一个混合的例子",
value: "demo.fix" value: "demo.fix"
}];Demo.WIDGET_CONFIG = [{ }];Demo.WIDGET_CONFIG = [{
@ -10260,6 +10265,54 @@ BI.shortcut("demo.tmp", Demo.Func);
}); });
BI.shortcut("demo.fix", Demo.Fix); 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(){ }());;(function(){
var model = Fix.define({ var model = Fix.define({
name: "原始属性", name: "原始属性",

66
dist/fix/fix.js vendored

@ -395,12 +395,12 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
} }
Observer.prototype.walk = function walk(obj) { Observer.prototype.walk = function walk(obj) {
return defineReactive(obj); return defineReactive(obj, this);
}; };
Observer.prototype.observeArray = function observeArray(items) { Observer.prototype.observeArray = function observeArray(items) {
for (var i = 0, l = items.length; i < l; i++) { 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; 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)) { if (!_.isObject(value)) {
return; return;
} }
var ob = void 0; var ob = void 0;
if (_.has(value, '__ob__') && value.__ob__ instanceof Observer) { if (_.has(value, '__ob__') && value.__ob__ instanceof Observer) {
ob = value.__ob__; 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); ob = new Observer(value);
} }
if (asRootData && ob) { ob.parent = parentObserver || ob.parent;
ob.vmCount++;
}
return ob; return ob;
} }
function defineReactive(obj, observer, shallow) { function defineReactive(obj, observer, shallow) {
var props = {}; var props = {};
var model = void 0;
_.each(obj, function (val, key) { _.each(obj, function (val, key) {
if (key in $$skipArray) { if (key in $$skipArray) {
return; return;
} }
var dep = observer && observer['__dep' + key] || new Dep(); var dep = observer && observer['__dep' + key] || new Dep();
observer && (observer['__dep' + key] = dep); observer && (observer['__dep' + key] = dep);
var childOb = !shallow && observe(val); var childOb = !shallow && observe(val, observer);
props[key] = { props[key] = {
enumerable: true, enumerable: true,
configurable: true, configurable: true,
@ -470,13 +468,25 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return; return;
} }
val = newVal; val = newVal;
childOb = !shallow && observe(newVal); childOb = !shallow && observe(newVal, observer);
obj[key] = childOb ? childOb.model : newVal; obj[key] = childOb ? childOb.model : newVal;
dep.notify(); 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; return val;
} }
var ob = target.__ob__; var ob = target.__ob__;
if (target._isVue || ob && ob.vmCount) {
return val;
}
if (!ob) { if (!ob) {
target[key] = val; target[key] = val;
return val; return val;
@ -517,9 +524,6 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return; return;
} }
var ob = target.__ob__; var ob = target.__ob__;
if (target._isVue || ob && ob.vmCount) {
return;
}
if (!_.has(target, key)) { if (!_.has(target, key)) {
return; return;
} }
@ -951,7 +955,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
options = options || {}; options = options || {};
options.user = true; options.user = true;
var exps = void 0; 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); var watcher = new Watcher(vm.model, expOrFn, _.bind(cb, vm), options);
if (options.immediate) { if (options.immediate) {
cb.call(vm, watcher.value); cb.call(vm, watcher.value);
@ -968,6 +972,32 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
if (_.has(operators, exp)) { if (_.has(operators, exp)) {
return; 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 () { var watcher = new Watcher(vm.model, exp, function () {
if (complete === true) { if (complete === true) {
return; return;

Loading…
Cancel
Save