guy 7 years ago
parent
commit
edc158996b
  1. 3
      demo/js/fix-2.0/globalwatcher.js
  2. 3
      dist/demo.js
  3. 196
      dist/fix/fix.js

3
demo/js/fix-2.0/globalwatcher.js

@ -13,6 +13,9 @@
return model;
},
watch: {
"*.*.n": function () {
debugger
},
"arr.**": function () {
debugger
},

3
dist/demo.js vendored

@ -10280,6 +10280,9 @@ BI.shortcut("demo.tmp", Demo.Func);
return model;
},
watch: {
"*.*.n": function () {
debugger
},
"arr.**": function () {
debugger
},

196
dist/fix/fix.js vendored

@ -400,7 +400,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
Observer.prototype.observeArray = function observeArray(items) {
for (var i = 0, l = items.length; i < l; i++) {
items[i] = observe(items[i], this).model;
items[i] = observe(items[i], this, i).model;
}
return items;
};
@ -422,7 +422,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
}
function observe(value, parentObserver) {
function observe(value, parentObserver, parentKey) {
if (!_.isObject(value)) {
return;
}
@ -433,6 +433,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
ob = new Observer(value);
}
ob.parent = parentObserver || ob.parent;
ob.parentKey = parentKey;
return ob;
}
@ -445,7 +446,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
var dep = observer && observer['__dep' + key] || new Dep();
observer && (observer['__dep' + key] = dep);
var childOb = !shallow && observe(val, observer);
var childOb = !shallow && observe(val, observer, key);
props[key] = {
enumerable: true,
configurable: true,
@ -468,21 +469,33 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return;
}
val = newVal;
childOb = !shallow && observe(newVal, observer);
childOb = !shallow && observe(newVal, observer, key);
obj[key] = childOb ? childOb.model : newVal;
dep.notify();
//触发a.*绑定的hooks
//触发a.*绑定的依赖
_.each(model.__ob__._deps, function (dep) {
dep.notify();
});
//触发a.**绑定的hooks
var parent = model.__ob__;
//触发a.**绑定的依赖
var parent = model.__ob__,
root = model.__ob__,
route = key;
while (parent) {
_.each(parent._globalDeps, function (dep) {
_.each(parent._scopeDeps, function (dep) {
dep.notify();
});
if (parent.parentKey != null) {
route = parent.parentKey + '.' + route;
}
root = parent;
parent = parent.parent;
}
for (var _key2 in root._globalDeps) {
var reg = new RegExp(_key2);
if (reg.test(route)) {
root._globalDeps[_key2].notify();
}
}
}
};
});
@ -868,6 +881,65 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
}
var VM = function () {
function VM(model) {
_classCallCheck(this, VM);
var vm = this;
if (model instanceof Observer || model instanceof VM) {
model = model.model;
}
if (_.has(model, '__ob__')) {
this.$$model = model;
} else {
this.options = model || {};
}
var keys = _.keys(this.$$model).concat(_.keys(this.computed));
var props = {};
var _loop = function _loop(i, len) {
var key = keys[i];
if (!(key in $$skipArray)) {
props[key] = {
enumerable: true,
configurable: true,
get: function get() {
return key in vm.$$computed ? vm.$$computed[key] : vm.$$model[key];
},
set: function set(val) {
if (!vm.$$model || !(key in vm.$$model)) {
return;
}
return vm.$$model[key] = val;
}
};
}
};
for (var i = 0, len = keys.length; i < len; i++) {
_loop(i, len);
}
this.model = createViewModel$1({}, props);
this.$$model && (this.model.__ob__ = this.$$model.__ob__);
initComputed(this, this.computed);
initMethods(this, this.actions);
this._init();
if (this.$$model) {
return this.model;
}
}
VM.prototype._init = function _init() {};
VM.prototype.destroy = function destroy() {
for (var _key3 in this._computedWatchers) {
this._computedWatchers[_key3].teardown();
}
};
return VM;
}();
var falsy$1;
var operators = {
'||': falsy$1,
@ -887,6 +959,12 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
return new Function('return ' + expr)();
}
function routeToRegExp(route) {
route = route.replace(/\*./g, '[a-zA-Z0-9_]+.');
return '^' + route + '$';
}
function watch(model, expOrFn, cb, options) {
if (isPlainObject(cb)) {
options = cb;
@ -915,7 +993,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
if (_.has(operators, exp)) {
return;
}
if (/\*\*$|\*$/.test(exp)) {
//a.**或a.*形式
if (/^[1-9a-zA-Z.]+(\*\*$|\*$)/.test(exp)) {
var isGlobal = /\*\*$/.test(exp);
if (isGlobal) {
//a.**的形式
@ -928,7 +1007,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var v = getter.call(model, model);
var dep = new Dep();
if (isGlobal) {
(v.__ob__._globalDeps || (v.__ob__._globalDeps = [])).push(dep);
(v.__ob__._scopeDeps || (v.__ob__._scopeDeps = [])).push(dep);
} else {
(v.__ob__._deps || (v.__ob__._deps = [])).push(dep);
}
@ -938,11 +1017,39 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}, cb);
watchers.push(function unwatchFn() {
w.teardown();
v.__ob__._globalDeps && remove(v.__ob__._globalDeps, dep);
v.__ob__._scopeDeps && remove(v.__ob__._scopeDeps, dep);
v.__ob__._deps && remove(v.__ob__._deps, dep);
});
return;
}
if (/\*\*/.test(exp)) {
throw new Error('not support');
}
//其他含有*的情况,如*.a,*.*.a,a.*.a.*
if (/\*/.test(exp)) {
//补全路径
var parent = model.__ob__.parent,
root = model.__ob__;
while (parent) {
exp = '*.' + exp;
root = parent;
parent = parent.parent;
}
var regStr = routeToRegExp(exp);
var _dep = new Dep();
root._globalDeps || (root._globalDeps = {});
root._globalDeps[regStr] = _dep;
var _w = new Watcher(model, function () {
_dep.depend();
return NaN;
}, cb);
watchers.push(function unwatchFn() {
_w.teardown();
root._globalDeps && delete root._globalDeps[regStr];
});
return;
}
var watcher = new Watcher(model, exp, function () {
if (complete === true) {
return;
@ -968,65 +1075,6 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return watchers;
}
var VM = function () {
function VM(model) {
_classCallCheck(this, VM);
var vm = this;
if (model instanceof Observer || model instanceof VM) {
model = model.model;
}
if (_.has(model, '__ob__')) {
this.$$model = model;
} else {
this.options = model || {};
}
var keys = _.keys(this.$$model).concat(_.keys(this.computed));
var props = {};
var _loop = function _loop(i, len) {
var key = keys[i];
if (!(key in $$skipArray)) {
props[key] = {
enumerable: true,
configurable: true,
get: function get() {
return key in vm.$$computed ? vm.$$computed[key] : vm.$$model[key];
},
set: function set(val) {
if (!vm.$$model || !(key in vm.$$model)) {
return;
}
return vm.$$model[key] = val;
}
};
}
};
for (var i = 0, len = keys.length; i < len; i++) {
_loop(i, len);
}
this.model = createViewModel$1({}, props);
this.$$model && (this.model.__ob__ = this.$$model.__ob__);
initComputed(this, this.computed);
initMethods(this, this.actions);
this._init();
if (this.$$model) {
return this.model;
}
}
VM.prototype._init = function _init() {};
VM.prototype.destroy = function destroy() {
for (var _key2 in this._computedWatchers) {
this._computedWatchers[_key2].teardown();
}
};
return VM;
}();
function toJSON(model) {
var result = void 0;
if (_.isArray(model)) {
@ -1036,9 +1084,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}
} else if (isPlainObject(model)) {
result = {};
for (var _key3 in model) {
if (!_.has($$skipArray, _key3)) {
result[_key3] = toJSON(model[_key3]);
for (var _key4 in model) {
if (!_.has($$skipArray, _key4)) {
result[_key4] = toJSON(model[_key4]);
}
}
} else {
@ -1055,7 +1103,6 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
exports.define = define;
exports.version = version;
exports.$$skipArray = $$skipArray;
exports.watch = watch;
exports.VM = VM;
exports.observerState = observerState;
exports.Observer = Observer;
@ -1064,6 +1111,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
exports.set = set;
exports.del = del;
exports.Watcher = Watcher;
exports.watch = watch;
exports.toJSON = toJSON;
exports.__esModule = true;

Loading…
Cancel
Save