Last active
December 16, 2015 20:39
-
-
Save shunjikonishi/5493754 to your computer and use it in GitHub Desktop.
Template of javascript-enum
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
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