Skip to content

Instantly share code, notes, and snippets.

@webketje
Last active December 5, 2016 22:15
Show Gist options
  • Save webketje/a5671305418f59b54d5e0ce93b6d5357 to your computer and use it in GitHub Desktop.
Save webketje/a5671305418f59b54d5e0ce93b6d5357 to your computer and use it in GitHub Desktop.
Superminimal virtual DOM utility
var vEl = function(element, options) {
var root = document.createElement(element);
for (var option in options) {
if (option === 'events') {
for (var event in options.events)
root.addEventListener(event, options.events[event], false);
} else if (option === 'children') {
for (var i = 0, len = options.children.length; i < len; i++)
root.appendChild(options.children[i]);
} else if (root.hasOwnProperty(option)) {
root[option] = options[option];
} else
root.setAttribute(option, options[option]);
}
return root;
};
var vText = function(text) {
return document.createTextNode(text);
};
var main = vEl('div', {
id: 'page-view',
children: [
vEl('div', {
id: 'breadcrumbs'
})
]
});
// returns div#page-view with div#breadcrumbs
// alias for querySelector ?
var bc = main.get('#breadcrumbs');
// alias for addEventListener ?
bc.on('click', function(e) {
});
@webketje
Copy link
Author

webketje commented Dec 5, 2016

Should events really be part of the function ? They can still be set after the dom element has been returned, and they don't really belong to the element, they are just bound to them by some external force (eg. the developer or app).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment