Browse Source

虚拟dom

es6
guy 3 years ago
parent
commit
c7f7cdb388
  1. 20
      examples/dev.html
  2. 48
      src/core/4.widget.js
  3. 1052
      src/core/8.snabdom.js
  4. 42
      src/core/element2Snabbdom.js

20
examples/dev.html

@ -48,7 +48,10 @@
type: "bi.vertical", type: "bi.vertical",
items: [{ items: [{
type: "bi.button", type: "bi.button",
text: store.model.text text: store.model.text,
handler: function () {
console.log("click");
}
}, { }, {
type: "bi.label", type: "bi.label",
text: store.model.text text: store.model.text
@ -62,7 +65,12 @@
var Widget = BI.inherit(BI.Widget, { var Widget = BI.inherit(BI.Widget, {
props: { props: {
updateMode: "auto" vdom: true
},
watch: {
text: function () {
this.reset();
}
}, },
setup: function () { setup: function () {
var child; var child;
@ -72,13 +80,13 @@
setInterval(function () { setInterval(function () {
store.toggle(); store.toggle();
}, 1000); }, 1000);
BI.watch("text", function () { // BI.watch("text", function () {
child.reset(); // child.reset();
}); // });
return function () { return function () {
return { return {
type: "bi.vertical", type: "bi.vertical",
vgap: 20, vgap: store.model.expand ? 20 : 30,
items: [{ items: [{
type: "demo.child", type: "demo.child",
ref: function (_ref) { ref: function (_ref) {

48
src/core/4.widget.js

@ -38,7 +38,7 @@
extraCls: "", extraCls: "",
cls: "", cls: "",
css: null, css: null,
updateMode: "manual" // manual / auto vdom: false
}); });
}, },
@ -165,6 +165,7 @@
this.element = BI.Widget._renderEngine.createElement(this); this.element = BI.Widget._renderEngine.createElement(this);
} }
this.element._isWidget = true; this.element._isWidget = true;
(this.element[0]._Widget = this.element[0]._Widget || []).push(this);
this._initCurrent(); this._initCurrent();
}, },
@ -216,20 +217,45 @@
els = [els]; els = [els];
} }
if (BI.isArray(els)) { if (BI.isArray(els)) {
if (this.options.vdom) {
this.vnode = this._renderVNode();
var div = document.createElement("div");
this.element.append(div);
BI.patchVNode(div, this.vnode);
} else {
BI.each(els, function (i, el) {
if (el) {
BI._lazyCreateWidget(el, {
element: self
});
}
});
}
}
this._mount();
if (this.__async === true && isMounted) {
callLifeHook(this, "mounted");
this.fireEvent(BI.Events.MOUNT);
}
},
_renderVNode: function () {
var render = BI.isFunction(this.options.render) ? this.options.render : this.render;
var els = render && render.call(this);
if (BI.isPlainObject(els)) {
els = [els];
}
if (BI.isArray(els)) {
var container = document.createElement("div");
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: container
}); });
} }
}); });
} }
this._mount(); return BI.Element2Snabbdom(container);
if (this.__async === true && isMounted) {
callLifeHook(this, "mounted");
this.fireEvent(BI.Events.MOUNT);
}
}, },
_setParent: function (parent) { _setParent: function (parent) {
@ -578,6 +604,12 @@
if (this.__async === true || this.__asking === true) { if (this.__async === true || this.__asking === true) {
return; return;
} }
if (this.options.vdom) {
var vnode = this._renderVNode();
BI.patchVNode(this.vnode, vnode);
this.vnode = vnode;
return;
}
// this._isMounted = false; // this._isMounted = false;
// this.purgeListeners(); // this.purgeListeners();
this._empty(); this._empty();

1052
src/core/8.snabdom.js

File diff suppressed because it is too large Load Diff

42
src/core/element2Snabbdom.js

@ -0,0 +1,42 @@
!function () {
var patch = BI.Snabbdom.init([BI.Snabbdom.attributesModule, BI.Snabbdom.classModule, BI.Snabbdom.datasetModule, BI.Snabbdom.propsModule, BI.Snabbdom.styleModule, BI.Snabbdom.eventListenersModule]);
BI.Element2Snabbdom = function (parentNode) {
if (parentNode.nodeType === 3) {
return parentNode.textContent;
}
var data = BI.jQuery._data(parentNode);
var on = {};
BI.each(data && data.events, function (eventName, events) {
on[eventName] = function () {
var ob = this, args = arguments;
BI.each(events, function (i, ev) {
ev.handler.apply(ob, args);
});
};
});
var style = parentNode.getAttribute("style");
// var claz = parentNode.getAttribute("class");
var vnode = BI.Snabbdom.h(parentNode.nodeName, {
class: BI.makeObject(parentNode.classList),
props: {
style: style
},
on: on,
hook: {
create: function () {
BI.each(parentNode._Widget, function (i, w) {
w.element = $(vnode.elm);
});
}
}
}, BI.map(parentNode.childNodes, function (i, childNode) {
return BI.Element2Snabbdom(childNode);
}));
return vnode;
};
BI.patchVNode = function (element, node) {
patch(element, node);
};
}();
Loading…
Cancel
Save