Non array event listeners can be replaced

pull/3/head
paldepind 10 years ago
parent b5db97812d
commit c67611f509

@ -4,6 +4,10 @@ function arrInvoker(arr) {
return function() { arr[0](arr[1]); };
}
function fnInvoker(arr) {
return function(ev) { arr[0](ev); };
}
function updateEventListeners(oldVnode, vnode) {
var name, cur, old, elm = vnode.elm,
oldOn = oldVnode.data.on || {}, on = vnode.data.on;
@ -12,11 +16,20 @@ function updateEventListeners(oldVnode, vnode) {
cur = on[name];
old = oldOn[name];
if (old === undefined) {
elm.addEventListener(name, is.array(cur) ? arrInvoker(cur) : cur);
} else if (is.array(old)) {
if (is.array(cur)) {
elm.addEventListener(name, arrInvoker(cur));
} else {
cur = [cur];
on[name] = cur;
elm.addEventListener(name, fnInvoker(cur));
}
} else if (old.length === 2) {
old[0] = cur[0]; // Deliberately modify old array since it's
old[1] = cur[1]; // captured in closure created with `arrInvoker`
on[name] = old;
} else {
old[0] = cur;
on[name] = old;
}
}
}

@ -1,6 +1,6 @@
{
"name": "snabbdom",
"version": "0.1.4",
"version": "0.1.5",
"description": "A virtual DOM library with focus on simplicity, modularity, powerful features and performance.",
"main": "snabbdom.js",
"directories": {

@ -418,16 +418,17 @@ describe('snabbdom', function() {
it('does not attach new listener', function() {
var result = [];
//function clicked(ev) { result.push(ev); }
var vnode1 = h('div', {on: {click: function(ev) { result.push(ev); }}}, [
var vnode1 = h('div', {on: {click: function(ev) { result.push(1); }}}, [
h('a', 'Click my parent'),
]);
var vnode2 = h('div', {on: {click: function(ev) { result.push(ev); }}}, [
var vnode2 = h('div', {on: {click: function(ev) { result.push(2); }}}, [
h('a', 'Click my parent'),
]);
patch(vnode0, vnode1);
elm.click();
patch(vnode1, vnode2);
elm.click();
assert.equal(1, result.length);
assert.deepEqual(result, [1, 2]);
});
it('does calls handler for function in array', function() {
var result = [];

Loading…
Cancel
Save