fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.0 KiB

7 years ago
;(function () {
7 years ago
function initWatch (vm, watch) {
7 years ago
vm._watchers || (vm._watchers = []);
for (var key in watch) {
7 years ago
var handler = watch[key];
7 years ago
if (BI.isArray(handler)) {
for (var i = 0; i < handler.length; i++) {
7 years ago
vm._watchers.push(createWatcher(vm, key, handler[i]));
7 years ago
}
} else {
7 years ago
vm._watchers.push(createWatcher(vm, key, handler));
7 years ago
}
}
}
7 years ago
function createWatcher (vm, keyOrFn, handler) {
return Fix.watch(vm.model, keyOrFn, _.bind(handler, vm), {
store: vm.store
7 years ago
});
7 years ago
}
7 years ago
var target = null;
const targetStack = [];
7 years ago
7 years ago
function pushTarget (_target) {
if (target) targetStack.push(target);
Fix.Model.target = target = _target;
7 years ago
}
7 years ago
function popTarget () {
Fix.Model.target = target = targetStack.pop();
7 years ago
}
var oldWatch = Fix.watch;
Fix.watch = function (model, expOrFn, cb, options) {
if (BI.isPlainObject(cb)) {
7 years ago
options = cb;
cb = cb.handler;
}
7 years ago
if (typeof cb === "string") {
cb = model[cb];
}
return oldWatch.call(this, model, expOrFn, function () {
pushTarget(options.store);
var res = cb.apply(this, arguments);
popTarget();
return res;
}, options);
7 years ago
};
function findStore (widget) {
var p = widget;
while (p) {
7 years ago
if (p.store || p.__cacheStore) {
7 years ago
break;
}
p = p._parent || (p.options && p.options.element);
}
if (p) {
7 years ago
widget.__cacheStore = p.store;
return p.__cacheStore || p.store;
7 years ago
}
}
7 years ago
var _init = BI.Widget.prototype._init;
BI.Widget.prototype._init = function () {
7 years ago
var needPop = false;
7 years ago
if (window.Fix && this._store) {
7 years ago
var store = findStore(this.options.element);
if (store) {
pushTarget(store);
needPop = true;
}
7 years ago
this.store = this._store();
7 years ago
needPop && popTarget();
needPop = false;
7 years ago
pushTarget(this.store);
7 years ago
if (this.store instanceof Fix.Model) {
7 years ago
this.model = this.store.model;
} else {
this.model = this.store;
}
initWatch(this, this.watch);
7 years ago
needPop = true;
7 years ago
}
_init.apply(this, arguments);
7 years ago
needPop && popTarget();
7 years ago
};
7 years ago
var unMount = BI.Widget.prototype.__d;
BI.Widget.prototype.__d = function () {
7 years ago
unMount.apply(this, arguments);
this.store && BI.isFunction(this.store.destroy) && this.store.destroy();
7 years ago
BI.each(this._watchers, function (i, unwatches) {
unwatches = BI.isArray(unwatches) ? unwatches : [unwatches];
BI.each(unwatches, function (j, unwatch) {
unwatch();
7 years ago
});
7 years ago
});
this._watchers && (this._watchers = []);
7 years ago
this.store && (this.store._parent = null, this.store = null);
7 years ago
};
7 years ago
7 years ago
_.each(["_mount"], function (name) {
var old = BI.Widget.prototype[name];
old && (BI.Widget.prototype[name] = function () {
7 years ago
this.store && pushTarget(this.store);
7 years ago
var res = old.apply(this, arguments);
7 years ago
this.store && popTarget();
7 years ago
return res;
});
7 years ago
});
7 years ago
_.each(["each", "map", "reduce", "reduceRight", "find", "filter", "reject", "every", "all", "some", "any", "max", "min",
"sortBy", "groupBy", "indexBy", "countBy", "partition",
"keys", "allKeys", "values", "pairs", "invert",
"mapObject", "findKey", "pick", "omit", "tap"], function (name) {
7 years ago
var old = BI[name];
7 years ago
BI[name] = function (obj, fn) {
7 years ago
return typeof fn === "function" ? old(obj, function (key, value) {
7 years ago
if (!(key in Fix.$$skipArray)) {
return fn.apply(this, arguments);
7 years ago
}
}) : old.apply(this, arguments);
7 years ago
};
7 years ago
});
7 years ago
}());