Skip to content

Instantly share code, notes, and snippets.

@jrhe
Created May 28, 2014 00:54
Show Gist options
  • Select an option

  • Save jrhe/d519d23ffba65f20ba9e to your computer and use it in GitHub Desktop.

Select an option

Save jrhe/d519d23ffba65f20ba9e to your computer and use it in GitHub Desktop.
Ember Simple Form + Select2 value binding (untested)
Ember.EasyForm.Select.reopen({
didInsertElement: function() {
// Listen for select2 value changes.
// Uses embers helper method to make sure this is bound correctly.
// See: http://balinterdi.com/2014/05/09/ember-dot-run-dot-bind.html
this.$().select2().on("change", Ember.run.bind(this, this.select2ValueChanged));
},
...
// When select2s value changes, update the components value
select2ValueChanged: function(e) {
this.set('value', e.val);
},
// When the components value changes, update select2s value
valueChanged: function(value) {
this.$().select2("val", value);
}.observes('value'),
...
willDestroyElement: function() {
// Unbind select2 change listener
this.$().select2().off("change");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment