From efbddc6f72d78f5ce760ac9f87a1db302af6642d Mon Sep 17 00:00:00 2001 From: bredele Date: Sat, 24 Sep 2016 20:10:24 -0600 Subject: [PATCH] listen event --- vomit.js | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/vomit.js b/vomit.js index b503e75..2a550e9 100644 --- a/vomit.js +++ b/vomit.js @@ -52,7 +52,7 @@ function bind(el, values) { var attrs = node.attributes // forEach faster? for(var i = 0, l = attrs.length; i < l; i++) { - attribute(attrs[i], values) + attribute(node, attrs[i], values) } } else text(node, values) }) @@ -67,18 +67,40 @@ function bind(el, values) { * @api private */ -function attribute(node, values) { +function attribute(parent, node, values) { var str = node.value - node.value = '' + var name = node.nodeName var arr = str.split('${0}') - if(arr[0]) spitup(node, arr[0]) - for(var i = 1, l = arr.length; i < l; i++) { - spitup(node, values.shift()) - var val = arr[i] - if(val) spitup(node, val) + node.value = '' + if(name.slice(0,2) == 'on') { + parent[name] = listen(values.splice(0, arr.length - 1)) + } else { + if(arr[0]) spitup(node, arr[0]) + for(var i = 1, l = arr.length; i < l; i++) { + spitup(node, values.shift()) + var val = arr[i] + if(val) spitup(node, val) + } } } +/** + * Create simple event listener from multiple functions. + * + * @note right now we make the assumption all placeholders + * in a listener should be functions + * + * @param {Array} arr + * @return {Function} + * @api private + */ + +function listen(arr) { + return function(event) { + var el = this + arr.map(fn => fn.call(this, event)) + } +} /** * Interpolate text nodes with values.