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

@ -452,7 +452,7 @@ describe('snabbdom', function() {
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() {
var result = [];
function cb(vnode) {
@ -463,7 +463,7 @@ describe('snabbdom', function() {
}
var vnode1 = h('div', [
h('span', 'First sibling'),
h('div', {oncreate: cb}, [
h('div', {hook: {create: cb}}, [
h('span', 'Child 1'),
h('span', 'Child 2'),
]),
@ -482,7 +482,7 @@ describe('snabbdom', function() {
}
var vnode1 = h('div', [
h('span', 'First sibling'),
h('div', {oninsert: cb}, [
h('div', {hook: {insert: cb}}, [
h('span', 'Child 1'),
h('span', 'Child 2'),
]),
@ -504,7 +504,7 @@ describe('snabbdom', function() {
}
var vnode1 = h('div', [
h('span', 'First sibling'),
h('div', {onremove: cb}, [
h('div', {hook: {remove: cb}}, [
h('span', 'Child 1'),
h('span', 'Child 2'),
]),

Loading…
Cancel
Save