Skip to content

Instantly share code, notes, and snippets.

@MichaelAz
Last active December 14, 2015 02:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelAz/5017456 to your computer and use it in GitHub Desktop.
Save MichaelAz/5017456 to your computer and use it in GitHub Desktop.
Bad - a javascript MVC microframework in 10 lines of code. Bad is to javascript MVC frameworks as McDonalds, Cheap Booze and Honey Boo Boo are to regular folks - it's so horrendously bad that it's good. In a just world this would not exist, but we live in a world of rushed spagheti code where this abomination fits perfectly. Client side MVC? Def…
// models is an object that defines the names of models, the names of their properties and their values which might be plain
// values or functions or whatever. pollingms is basically the refresh rate on the model. Views are defined in Handlebars.
// EXAMPLE OF USAGE IN THE NEXT FILE
function bad (models, pollingms) {
$(function() {
var originalHtml = $("html").html();
window.setInterval(function() {
$("html").html(originalHtml);
$('script[type*="text/x-handlebars-template"]').replaceWith(function() {
return Handlebars.compile($(this).text())(models[$(this).attr("model")]);
});
}, pollingms);
});
}
<!-- This will create a page with a random number that changes every second -->
<html>
<head>
<script type="text/javascript" src="../Dependencies/jquery-1.9.1.js"></script>
<script type="text/javascript" src="../Dependencies/handlebars.js"></script>
<script type="text/javascript">
bad({
"testModel" : {
"testProperty" : function() {
return Math.random();
}
}
},1000);
</script>
</head>
<body>
<script type="text/x-handlebars-template" model="testModel">
{{testProperty}}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment