Created
July 8, 2012 20:25
-
-
Save devinrhode2/3072669 to your computer and use it in GitHub Desktop.
Why not hookup mustache templates to auto-update?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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