Skip to content

Instantly share code, notes, and snippets.

@shunjikonishi
Last active December 16, 2015 20:39
Show Gist options
  • Save shunjikonishi/5493754 to your computer and use it in GitHub Desktop.
Save shunjikonishi/5493754 to your computer and use it in GitHub Desktop.
Template of javascript-enum
function Enum(values) {
for (var i=0; i<values.length; i++) {
var v = values[i];
this[v.name] = v;
}
$.extend(this, {
"fromCode" : function(v) {
for (var i=0; i<values.length; i++) {
if (values[i].code == v) return values[i];
}
return null;
},
"fromName" : function(v) {
for (var i=0; i<values.length; i++) {
if (values[i].name == v) return values[i];
}
return null;
},
"fromText" : function(v) {
for (var i=0; i<values.length; i++) {
if (values[i].text == v) return values[i];
}
return null;
},
"bindSelect" : function(el) {
for (var i=0; i<values.length; i++) {
var v = values[i];
var option = $("<option></option>");
option.attr("value", v.name);
option.text(v.text);
el.append(option);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment