Created
March 9, 2016 13:41
-
-
Save andreisebastianc/5f7c20f98822f21af850 to your computer and use it in GitHub Desktop.
Duck typing dropdowns in javascript
This file contains hidden or 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
class Select { | |
constructor(options, element, selectedOptionsIndex=0, customBehavior={}) { | |
this.options = options; | |
this.selectedOptionsIndex = selectedOptionsIndex; | |
this.el = element; | |
// alternative you'd use _.extend | |
Object.keys(customBehavior).forEach(key => { this[key] = customBehavior[key] }); | |
} | |
render() { | |
this.el.append(this.options.map(this.renderOption)); | |
} | |
renderOption(option) { | |
return `<li>${option}</li>`; | |
} | |
} | |
specialSelect = new Select(someArray, $('.placeholder'), 0, { | |
renderOption: function(option) { | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment