Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating bootstrap-select to work with Ember

I'm trying to get bootstrap-select working with Ember.js. Something about Ember's management of view objects that prevents it from working as intended.

JSFiddle

$("select").selectpicker() works okay for normal selects, and replaces Ember.Select views HTML but the Ember ones are broken.

The .ember-view class is what's responsible here and you can hack it to remove that class from the select and options to make bootstrap-select work, but then of course ember no longer pays attention to it, defeating the purpose.

Anyone understand EmberView enough to make this work? Should I try and override Ember.Select? Or does bootstrap-select need changing? I'm adrift in a sea of source code.

like image 359
The Worker Ant Avatar asked May 15 '13 13:05

The Worker Ant


1 Answers

This is not the bootstrap select component but the select2 (much nicer :) and this is how we have set it up to play nicely with the ember select view:

App.Select2SelectView = Ember.Select.extend({
  prompt: 'Please select...',
  classNames: ['input-xlarge'],

  didInsertElement: function() {
    Ember.run.scheduleOnce('afterRender', this, 'processChildElements');
  },

  processChildElements: function() {
    this.$().select2({
        // do here any configuration of the
        // select2 component
    });
  },

  willDestroyElement: function () {
    this.$().select2("destroy");
  }
})

and then we use it like so:

<div class="controls">
    {{view App.Select2SelectView
        id="mySelect"
        contentBinding="App.staticData"
        optionValuePath="content.id"
        optionLabelPath="content.label"
        selectionBinding="controller.selectedId"}}
</div>

I think although it is for the select2 component you can use the same hooks didInsertElement and willDestroyElement for the bootstrap select component.

And if you really need the bootstrap select, then maybe this is something for you: https://github.com/emberjs-addons/ember-bootstrap

Hope it helps

like image 92
intuitivepixel Avatar answered Sep 18 '22 18:09

intuitivepixel