Browse Source

生命周期严格按照beforemount render mounted的顺序执行

es6
guy 3 years ago
parent
commit
1835928fde
  1. 140
      dist/fix/fix.compact.ie.js
  2. 140
      dist/fix/fix.compact.js
  3. 27
      src/core/4.widget.js

140
dist/fix/fix.compact.ie.js vendored

@ -163,76 +163,76 @@
needPop && popTarget(); needPop && popTarget();
}; };
BI.Widget.prototype._initElement = function () { // BI.Widget.prototype._initElement = function () {
var self = this; // var self = this;
var render = BI.isFunction(this.options.render) ? this.options.render : this.render; // var render = BI.isFunction(this.options.render) ? this.options.render : this.render;
var els; // var els;
if (this.options.updateMode === "auto" && this._store) { // if (this.options.updateMode === "auto" && this._store) {
// 自动更新模式 // // 自动更新模式
var childComponents = {}; // var childComponents = {};
var rendered = false; // var rendered = false;
this._watchers.push(Fix.watch(this.model, function () { // this._watchers.push(Fix.watch(this.model, function () {
if (rendered) { // if (rendered) {
var newEls = render && render.call(this); // var newEls = render && render.call(this);
BI.each(childComponents, function (i, childComponent) { // BI.each(childComponents, function (i, childComponent) {
if (childComponent.component instanceof BI.Layout) { // if (childComponent.component instanceof BI.Layout) {
return; // 布局的过滤掉 // return; // 布局的过滤掉
} // }
var nextProps = BI.get([newEls], childComponent.path); // var nextProps = BI.get([newEls], childComponent.path);
if (nextProps) { // if (nextProps) {
var shouldUpdate = childComponent.component.shouldUpdate && childComponent.component.shouldUpdate(nextProps); // var shouldUpdate = childComponent.component.shouldUpdate && childComponent.component.shouldUpdate(nextProps);
childComponent.component._update(nextProps, shouldUpdate); // childComponent.component._update(nextProps, shouldUpdate);
childComponent.props = BI.extend(childComponent.props, nextProps); // childComponent.props = BI.extend(childComponent.props, nextProps);
} // }
}); // });
} else { // } else {
els = render && render.call(this); // els = render && render.call(this);
//
function traverse (parent, path) { // function traverse (parent, path) {
BI.each(parent, function (i, child) { // BI.each(parent, function (i, child) {
var childPath = path.concat(i); // var childPath = path.concat(i);
if (BI.isArray(child)) { // if (BI.isArray(child)) {
traverse(child, childPath); // traverse(child, childPath);
} else if (BI.isPlainObject(child)) { // } else if (BI.isPlainObject(child)) {
if (child.type) { // if (child.type) {
child.__ref = function (_ref) { // child.__ref = function (_ref) {
if (_ref) { // if (_ref) {
var comp = childComponents[this.getName()] = {}; // var comp = childComponents[this.getName()] = {};
comp.component = _ref; // comp.component = _ref;
comp.props = child; // comp.props = child;
comp.path = childPath; // comp.path = childPath;
} else { // } else {
delete childComponents[this.getName()]; // delete childComponents[this.getName()];
} // }
}; // };
} // }
traverse(child, childPath); // traverse(child, childPath);
} // }
}); // });
} // }
//
traverse([els], []); // traverse([els], []);
rendered = true; // rendered = true;
} // }
})); // }));
} else { // } else {
els = render && render.call(this); // els = render && render.call(this);
} // }
if (BI.isPlainObject(els)) { // if (BI.isPlainObject(els)) {
els = [els]; // els = [els];
} // }
if (BI.isArray(els)) { // if (BI.isArray(els)) {
BI.each(els, function (i, el) { // BI.each(els, function (i, el) {
if (el) { // if (el) {
BI._lazyCreateWidget(el, { // BI._lazyCreateWidget(el, {
element: self // element: self
}); // });
} // }
}); // });
} // }
// if (this._isRoot === true || !(this instanceof BI.Layout)) { // // if (this._isRoot === true || !(this instanceof BI.Layout)) {
this._mount(); // this._mount();
}; // };
var unMount = BI.Widget.prototype.__d; var unMount = BI.Widget.prototype.__d;
BI.Widget.prototype.__d = function () { BI.Widget.prototype.__d = function () {

140
dist/fix/fix.compact.js vendored

@ -163,76 +163,76 @@
needPop && popTarget(); needPop && popTarget();
}; };
BI.Widget.prototype._initElement = function () { // BI.Widget.prototype._initElement = function () {
var self = this; // var self = this;
var render = BI.isFunction(this.options.render) ? this.options.render : this.render; // var render = BI.isFunction(this.options.render) ? this.options.render : this.render;
var els; // var els;
if (this.options.updateMode === "auto" && this._store) { // if (this.options.updateMode === "auto" && this._store) {
// 自动更新模式 // // 自动更新模式
var childComponents = {}; // var childComponents = {};
var rendered = false; // var rendered = false;
this._watchers.push(Fix.watch(this.model, function () { // this._watchers.push(Fix.watch(this.model, function () {
if (rendered) { // if (rendered) {
var newEls = render && render.call(this); // var newEls = render && render.call(this);
BI.each(childComponents, function (i, childComponent) { // BI.each(childComponents, function (i, childComponent) {
if (childComponent.component instanceof BI.Layout) { // if (childComponent.component instanceof BI.Layout) {
return; // 布局的过滤掉 // return; // 布局的过滤掉
} // }
var nextProps = BI.get([newEls], childComponent.path); // var nextProps = BI.get([newEls], childComponent.path);
if (nextProps) { // if (nextProps) {
var shouldUpdate = childComponent.component.shouldUpdate && childComponent.component.shouldUpdate(nextProps); // var shouldUpdate = childComponent.component.shouldUpdate && childComponent.component.shouldUpdate(nextProps);
childComponent.component._update(nextProps, shouldUpdate); // childComponent.component._update(nextProps, shouldUpdate);
childComponent.props = BI.extend(childComponent.props, nextProps); // childComponent.props = BI.extend(childComponent.props, nextProps);
} // }
}); // });
} else { // } else {
els = render && render.call(this); // els = render && render.call(this);
//
function traverse (parent, path) { // function traverse (parent, path) {
BI.each(parent, function (i, child) { // BI.each(parent, function (i, child) {
var childPath = path.concat(i); // var childPath = path.concat(i);
if (BI.isArray(child)) { // if (BI.isArray(child)) {
traverse(child, childPath); // traverse(child, childPath);
} else if (BI.isPlainObject(child)) { // } else if (BI.isPlainObject(child)) {
if (child.type) { // if (child.type) {
child.__ref = function (_ref) { // child.__ref = function (_ref) {
if (_ref) { // if (_ref) {
var comp = childComponents[this.getName()] = {}; // var comp = childComponents[this.getName()] = {};
comp.component = _ref; // comp.component = _ref;
comp.props = child; // comp.props = child;
comp.path = childPath; // comp.path = childPath;
} else { // } else {
delete childComponents[this.getName()]; // delete childComponents[this.getName()];
} // }
}; // };
} // }
traverse(child, childPath); // traverse(child, childPath);
} // }
}); // });
} // }
//
traverse([els], []); // traverse([els], []);
rendered = true; // rendered = true;
} // }
})); // }));
} else { // } else {
els = render && render.call(this); // els = render && render.call(this);
} // }
if (BI.isPlainObject(els)) { // if (BI.isPlainObject(els)) {
els = [els]; // els = [els];
} // }
if (BI.isArray(els)) { // if (BI.isArray(els)) {
BI.each(els, function (i, el) { // BI.each(els, function (i, el) {
if (el) { // if (el) {
BI._lazyCreateWidget(el, { // BI._lazyCreateWidget(el, {
element: self // element: self
}); // });
} // }
}); // });
} // }
// if (this._isRoot === true || !(this instanceof BI.Layout)) { // // if (this._isRoot === true || !(this instanceof BI.Layout)) {
this._mount(); // this._mount();
}; // };
var unMount = BI.Widget.prototype.__d; var unMount = BI.Widget.prototype.__d;
BI.Widget.prototype.__d = function () { BI.Widget.prototype.__d = function () {

27
src/core/4.widget.js

@ -7,7 +7,7 @@
*/ */
!(function () { !(function () {
function callLifeHook(self, life) { function callLifeHook (self, life) {
var hook = self.options[life] || self[life]; var hook = self.options[life] || self[life];
if (hook) { if (hook) {
var hooks = BI.isArray(hook) ? hook : [hook]; var hooks = BI.isArray(hook) ? hook : [hook];
@ -98,8 +98,9 @@
_initRender: function () { _initRender: function () {
var self = this; var self = this;
function render() { function render () {
if (self.options.beforeRender || self.beforeRender) { if (self.options.beforeRender || self.beforeRender) {
this.__async = true;
(self.options.beforeRender || self.beforeRender).call(self, BI.bind(self._render, self)); (self.options.beforeRender || self.beforeRender).call(self, BI.bind(self._render, self));
} else { } else {
self._render(); self._render();
@ -109,9 +110,6 @@
if (this.options.beforeInit || this.beforeInit) { if (this.options.beforeInit || this.beforeInit) {
this.__asking = true; this.__asking = true;
(this.options.beforeInit || this.beforeInit).call(this, render); (this.options.beforeInit || this.beforeInit).call(this, render);
if (this.__asking === true) {
this.__async = true;
}
} else { } else {
render(); render();
} }
@ -123,6 +121,7 @@
this._initElement(); this._initElement();
this._initEffects(); this._initEffects();
callLifeHook(this, "created"); callLifeHook(this, "created");
this.__async = false;
}, },
_initCurrent: function () { _initCurrent: function () {
@ -204,6 +203,8 @@
_initElement: function () { _initElement: function () {
var self = this; var self = this;
var isMounted = this._isMounted;
this.__async === true && isMounted && callLifeHook(this, "beforeMount");
var render = BI.isFunction(this.options.render) ? this.options.render : this.render; var render = BI.isFunction(this.options.render) ? this.options.render : this.render;
var els = render && render.call(this); var els = render && render.call(this);
if (BI.isPlainObject(els)) { if (BI.isPlainObject(els)) {
@ -218,9 +219,9 @@
} }
}); });
} }
// if (this._isRoot === true || !(this instanceof BI.Layout)) {
this._mount(); this._mount();
// }
this.__async === true && isMounted && callLifeHook(this, "mounted");
}, },
_setParent: function (parent) { _setParent: function (parent) {
@ -242,7 +243,7 @@
return false; return false;
} }
layer = layer || 0; layer = layer || 0;
lifeHook !== false && callLifeHook(this, "beforeMount"); lifeHook !== false && !this.__async && callLifeHook(this, "beforeMount");
this._isMounted = true; this._isMounted = true;
for (var key in this._children) { for (var key in this._children) {
var child = this._children[key]; var child = this._children[key];
@ -266,8 +267,10 @@
var child = this._children[key]; var child = this._children[key];
child.__afterMount && child.__afterMount(lifeHook, predicate); child.__afterMount && child.__afterMount(lifeHook, predicate);
} }
lifeHook !== false && callLifeHook(this, "mounted"); if (lifeHook !== false && !this.__async) {
this.fireEvent(BI.Events.MOUNT); callLifeHook(this, "mounted");
this.fireEvent(BI.Events.MOUNT);
}
predicate && predicate(this); predicate && predicate(this);
} }
}, },
@ -593,12 +596,12 @@
BI.Widget.context = context = contextStack.pop(); BI.Widget.context = context = contextStack.pop();
}; };
function pushTarget(_current) { function pushTarget (_current) {
if (current) currentStack.push(current); if (current) currentStack.push(current);
BI.Widget.current = current = _current; BI.Widget.current = current = _current;
} }
function popTarget() { function popTarget () {
BI.Widget.current = current = currentStack.pop(); BI.Widget.current = current = currentStack.pop();
} }

Loading…
Cancel
Save