select2 4.0

This commit is contained in:
Matthew Dillon 2015-11-17 12:33:30 -07:00
parent b19913aece
commit 4e5a91f776
3 changed files with 34 additions and 21 deletions

View file

@ -1,6 +1,6 @@
import Ember from 'ember';
const { Component } = Ember;
const { Component, get, run: { schedule } } = Ember;
export default Component.extend({
tagName: 'select',
@ -11,24 +11,43 @@ export default Component.extend({
options: null,
selected: null,
nameAttr: null,
placeholder: null,
change: function() {
this.attrs.update(this.$().select2('val'));
},
let selectedInComponent = this.get('selected');
let selectedInWidget = this.$().val();
didInsertElement: function() {
if (this.get('placeholder')) {
this.$().select2({
placeholder: this.get('placeholder'),
});
} else {
this.$().select2();
if (this.get('multiple')) {
if (selectedInWidget === null) {
selectedInWidget = [];
}
selectedInComponent = selectedInComponent.toString();
selectedInWidget = selectedInWidget.toString();
}
// We need this to prevent an infinite loop of afterRender -> change.
if (selectedInComponent !== selectedInWidget) {
this.attrs.update(this.$().val());
}
},
didInsertElement: function() {
let options = {};
options.placeholder = this.get('placeholder');
options.templateResult = function(item) {
if (!item.disabled) {
const text = get(item, 'element.innerHTML');
const $item = Ember.$(`<span>${text}</span>`);
return $item;
}
};
this.$().select2(options);
},
didRender: function() {
Ember.run.schedule('afterRender', this, function() {
this.$().select2('val', this.get('selected'));
const selected = this.get('selected');
schedule('afterRender', this, function() {
this.$().val(selected).trigger('change');
});
},