guy 7 years ago
parent
commit
5a3de388ab
  1. 74
      dist/fix/fix.compact.js

74
dist/fix/fix.compact.js vendored

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