Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Why not hookup mustache templates to auto-update?
/* by Devin Rhode (@Devinrhode2)
Mustache templates are a one-time operation.
I realized this doesn't have a to be, and having them auto-update is *extremely trivial*
So, we have this mustache:
<p>Hello, {{username}}</p>
Instead of just outputting:
<p>Hello, Bob</p>
We could do:
<p>Hello, <span> id="username">Bob</span></p>
Got new data for the view? Don't refresh the page, we just need the new JSON for the view: */
function receiveUpdate(update){ //update is the new JSON for the view.
for (var key in update) { //for each data item that needs to be updated.
document.getElementById(key).innerHTML = update[key]; //get that data item by id, and update it!
}
});
/*
We don't have to reload the page to refill the template with the updated data. All we have to do is get the updated data.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.