Skip to content

Instantly share code, notes, and snippets.

@spooky
Created September 21, 2018 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spooky/2572b01e2030cdf7b2fae9487b1f5c26 to your computer and use it in GitHub Desktop.
Save spooky/2572b01e2030cdf7b2fae9487b1f5c26 to your computer and use it in GitHub Desktop.
tiny obj to template binder
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>refresh</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
body {
font-size: 24px;
}
div {
margin: 3em;
text-align: center;
}
</style>
</head>
<body>
<div>
<span class="foo"></span>
<strong id="bar"></strong>
<span class="foo"></span>
</div>
<script>
function refresh(data, mapper) {
for (var m in mapper) mapper[m].set && "function" == typeof mapper[m].set && mapper[m].el && data[m] && mapper[m].set(mapper[m].el, data[m])
};
function innerTextOnAll(id) {
return {
el: document.querySelectorAll(id),
set: function(el, val) {
el.forEach(e => e.innerText = val);
}
}
};
const data = {
foo: "hello",
bar: "world"
},
mapper = {
foo: innerTextOnAll(".foo"),
bar: innerTextOnAll("#bar")
};
refresh(data, mapper);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment