Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Created July 8, 2012 20:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save devinrhode2/3072669 to your computer and use it in GitHub Desktop.
Save devinrhode2/3072669 to your computer and use it in GitHub Desktop.
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