Last active
August 29, 2015 14:11
-
-
Save samuelsimoes/0b7249affafbdcdf32ae to your computer and use it in GitHub Desktop.
Some Handlebars helpers
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
Handlebars.registerHelper("bootstrapField", function(context, options) { | |
optionsHash = options.hash; | |
var markup = "<div class='control-group " + (optionsHash.containerClass || "") + "'>", | |
attrs = _.omit(optionsHash, ["containerClass", "label", "required", "helpText"]); | |
markup += "<label class='control-label"; | |
if (optionsHash.required) { | |
markup += " required"; | |
} | |
markup += "'>"; | |
if (optionsHash.required) { | |
markup += "<abbr title='Obrigatório'>*</abbr> "; | |
} | |
markup += optionsHash.label + "</label>"; | |
markup += "<div class='control-group'>"; | |
if (options.fn) { | |
markup += options.fn(context); | |
} else { | |
markup += "<input type='text'"; | |
_.each(attrs, function(value, key) { | |
markup += " " + key + "='" + (value || "") + "' "; | |
}); | |
markup += "/>"; | |
} | |
if (optionsHash.helpText) { | |
markup += "<span class='help-inline'>" + optionsHash.helpText + "</span>"; | |
} | |
markup += "</div></div>"; | |
return new Handlebars.SafeString(markup); | |
}); |
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
// {{ select states selectedValue=currentState labelAttribute="acronym" class="mySelect" }} | |
Handlebars.registerHelper("select", function(optionsToSelect, options) { | |
var optionsHash = options.hash, | |
attrs = _.omit(optionsHash, [ | |
"labelAttribute", | |
"idAttribute", | |
"disableOptions", | |
"choosePrompt", | |
"selectedValue" | |
]), | |
markup = "<select"; | |
_.each(attrs, function(value, key) { | |
markup += " " + key + "='" + (value || "") + "' "; | |
}); | |
markup += ">"; | |
if (!optionsHash.choosePrompt && optionsHash.choosePrompt !== "") { | |
markup += "<option>" + optionsHash.choosePrompt + "</option>"; | |
} | |
markup += Handlebars.helpers.selectOptions(optionsToSelect, options); | |
markup += "</select>"; | |
return new Handlebars.SafeString(markup); | |
}); |
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
// {{ selectOptions states selectedValue=currentState labelAttribute="acronym" }} | |
Handlebars.registerHelper("selectOptions", function(optionsToSelect, options) { | |
options = options.hash; | |
var labelAttribute = options.labelAttribute ? options.labelAttribute : "name", | |
idAttribute = options.idAttribute ? options.idAttribute : "id", | |
disableOptions = options.disableOptions ? options.disableOptions : []; | |
var optionsMarkup = _.inject(optionsToSelect, function (memo, option) { | |
var optionMarkup = "<option value='" + option[idAttribute] + "'"; | |
if (_.contains(disableOptions, option[idAttribute])) { | |
optionMarkup += " disabled='disabled' "; | |
} | |
if (options.selectedValue && (options.selectedValue.toString() == option[idAttribute].toString())) { | |
optionMarkup += " selected "; | |
} | |
optionMarkup += ">" + option[labelAttribute] + "</option>"; | |
memo += optionMarkup; | |
return memo; | |
}, ""); | |
return new Handlebars.SafeString(optionsMarkup); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment