Skip to content

Using select2 with easyForm

sandreas edited this page Feb 6, 2014 · 1 revision

Here is some sample code, how you could integrate the select2-library with ember-easyForm:

Ember.EasyForm.Select.reopen({
	didInsertElement : function() {
		var context = this;
		Ember.run.scheduleOnce('afterRender', this, 'didRenderElement');
		Ember.addObserver(this, 'controller.' + this.get('collection'),
				function() {
					Ember.run.scheduleOnce('afterRender', context,
							context.elementToWidget);
				});
	},
	didRenderElement: function() {
		if(!this.$()) {
			return;
		}
		
		// fix multiple selects: no selected option on load		
		if(this.$().prop('multiple') && this.$().find('option:selected').length == 0) {
			this.$().val('').change();
		}
		
		this.$().select2({
			placeholder: this.get('placeholder')
		});
		
	},
	willDestroyElement : function() {
		this.$().select2("destroy");
	}
});