Skip to content

Instantly share code, notes, and snippets.

@andreisebastianc
Created March 9, 2016 13:41
Show Gist options
  • Save andreisebastianc/5f7c20f98822f21af850 to your computer and use it in GitHub Desktop.
Save andreisebastianc/5f7c20f98822f21af850 to your computer and use it in GitHub Desktop.
Duck typing dropdowns in javascript
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