Move lifecycle hook to `hook` obj in `data`

pull/2/head
paldepind 10 years ago
parent eda5497f17
commit d819a713f0

@ -58,8 +58,11 @@ function init(modules) {
elm.appendChild(document.createTextNode(vnode.text)); elm.appendChild(document.createTextNode(vnode.text));
} }
for (i = 0; i < createCbs.length; ++i) createCbs[i](emptyNode, vnode); for (i = 0; i < createCbs.length; ++i) createCbs[i](emptyNode, vnode);
if (vnode.data.oncreate) vnode.data.oncreate(vnode); i = vnode.data.hook; // Reuse variable
if (vnode.data.oninsert) insertedVnodeQueue.push(vnode); if (!isUndef(i)) {
if (i.create) i.create(vnode);
if (i.insert) insertedVnodeQueue.push(vnode);
}
} else { } else {
elm = vnode.elm = document.createTextNode(vnode.text); elm = vnode.elm = document.createTextNode(vnode.text);
} }
@ -83,8 +86,8 @@ function init(modules) {
for (; startIdx <= endIdx; ++startIdx) { for (; startIdx <= endIdx; ++startIdx) {
var ch = vnodes[startIdx]; var ch = vnodes[startIdx];
if (!isUndef(ch)) { if (!isUndef(ch)) {
if (ch.data.onremove) { if (ch.data.hook && ch.data.hook.remove) {
ch.data.onremove(ch, parentElm.removeChild.bind(parentElm, ch.elm)); ch.data.hook.remove(ch, parentElm.removeChild.bind(parentElm, ch.elm));
} else { } else {
parentElm.removeChild(ch.elm); parentElm.removeChild(ch.elm);
} }
@ -168,7 +171,7 @@ function init(modules) {
insertedVnodeQueue = []; insertedVnodeQueue = [];
patchVnode(oldVnode, vnode); patchVnode(oldVnode, vnode);
for (var i = 0; i < insertedVnodeQueue.length; ++i) { for (var i = 0; i < insertedVnodeQueue.length; ++i) {
insertedVnodeQueue[i].data.oninsert(insertedVnodeQueue[i]); insertedVnodeQueue[i].data.hook.insert(insertedVnodeQueue[i]);
} }
insertedVnodeQueue = undefined; insertedVnodeQueue = undefined;
return vnode; return vnode;

@ -452,7 +452,7 @@ describe('snabbdom', function() {
assert.deepEqual(result, [1, 2]); assert.deepEqual(result, [1, 2]);
}); });
}); });
describe('custom events', function() { describe('lifecycle hooks', function() {
it('calls `create` listener before inserted into parent but after children', function() { it('calls `create` listener before inserted into parent but after children', function() {
var result = []; var result = [];
function cb(vnode) { function cb(vnode) {
@ -463,7 +463,7 @@ describe('snabbdom', function() {
} }
var vnode1 = h('div', [ var vnode1 = h('div', [
h('span', 'First sibling'), h('span', 'First sibling'),
h('div', {oncreate: cb}, [ h('div', {hook: {create: cb}}, [
h('span', 'Child 1'), h('span', 'Child 1'),
h('span', 'Child 2'), h('span', 'Child 2'),
]), ]),
@ -482,7 +482,7 @@ describe('snabbdom', function() {
} }
var vnode1 = h('div', [ var vnode1 = h('div', [
h('span', 'First sibling'), h('span', 'First sibling'),
h('div', {oninsert: cb}, [ h('div', {hook: {insert: cb}}, [
h('span', 'Child 1'), h('span', 'Child 1'),
h('span', 'Child 2'), h('span', 'Child 2'),
]), ]),
@ -504,7 +504,7 @@ describe('snabbdom', function() {
} }
var vnode1 = h('div', [ var vnode1 = h('div', [
h('span', 'First sibling'), h('span', 'First sibling'),
h('div', {onremove: cb}, [ h('div', {hook: {remove: cb}}, [
h('span', 'Child 1'), h('span', 'Child 1'),
h('span', 'Child 2'), h('span', 'Child 2'),
]), ]),

Loading…
Cancel
Save