Skip to content

Instantly share code, notes, and snippets.

@seanchas116
Last active April 12, 2016 11:47
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 seanchas116/a0d265ec34d43c62c01841e29ad5ef99 to your computer and use it in GitHub Desktop.
Save seanchas116/a0d265ec34d43c62c01841e29ad5ef99 to your computer and use it in GitHub Desktop.
Simple list data-binding utility idea
<div class="todo-list">
<div class="todo">
<input type="checkbox" class="done">
<h3 class="title">Title</h3>
</div>
</div>
const todoBinder = bindToChildren({
element: document.querySelector(".todo-list"),
key: (value) => value.id,
// called on first insertion
onAdd: (elem, value) => {
return new TodoController(elem);
},
// called on data update
onUpdate: (elem, value, controller) => {
controller.setTodo(value);
},
// called on removal
onRemove: (elem, value, controller) => {
controller.dispose();
}
});
binder.values = [
{title: "Task", done: false, id: 0},
{title: "Another Task", done: true, id: 1},
];
binder.values = [
{title: "Task", done: true, id: 0},
{title: "Another Task", done: true, id: 1},
{title: "New Task", done: false, id: 2},
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment