-
-
Save bobthecow/1978729 to your computer and use it in GitHub Desktop.
Checking radio with Mustache
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> | |
<script type="text/javascript" src="js/mustache.js"></script> | |
</head> | |
<body> | |
<div id="formWrapper"> | |
</div> | |
<script id="tmpl" type="x-template"> | |
<form> | |
<label><input type="radio" name="gender" value="male"{{#maleIsDefault}} checked{{/maleIsDefault}}> Male</label> | |
<label><input type="radio" name="gender" value="female"{{#femaleIsDefault}} checked{{/femaleIsDefault}}> Female</label> | |
</form> | |
</script> | |
<script type="text/javascript"> | |
var data = { | |
gender: 'female', | |
maleIsDefault: function() { | |
return this.gender == 'male'; | |
}, | |
femaleIsDefault: function() { | |
return this.gender == 'female'; | |
} | |
}; | |
var template = $("#tmpl").html(); | |
var result = Mustache.render(template, data); | |
$("#formWrapper").html(result); | |
</script> | |
</body> | |
</html> |
var view = {
title: "Joe",
calc: function () {
return 2 + 4;
}
};
var output = Mustache.render("{{title}} spends {{calc}}", view);
why it only result as 'spends '?
who can help me...
I found that your code is in .html,but mine is in .mustache file.
can't understand why is this even working. what i did is something similar to this :
if output.gender == 'male' do maleIsDefault, else do femaleIsDefault, which will then assign values to male or female is default, anything between the opening and closing tag will be enabled..
but i want to know how this works
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, less of a hack than my first attempt :)