-
-
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…
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
// 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 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
<!-- 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