Skip to content

Instantly share code, notes, and snippets.

@sirvine
Created June 22, 2012 17:37
Show Gist options
  • Save sirvine/2974141 to your computer and use it in GitHub Desktop.
Save sirvine/2974141 to your computer and use it in GitHub Desktop.
Ember.js Chosen Implementation
// This implementation assumes a router that calls connectOutlet on the current parent view's controller along the lines of:
// newThingController.connectOutlet({
// name: 'chosenSelect'
// })
//
//...which in turn assumes you've got App.ChosenSelectController with the sorting mixin along the lines of:
// App.ChosenSelectController = Ember.ArrayController.extend({
// content: App.store.findAll(App.Client),
// sortProperties: ['name'],
// sortAscending: true
// });
//
// ...and an {{outlet}} in the newThing.handlebars template
//
App.UnboundSelectOption = Ember.SelectOption.extend({
template: Ember.Handlebars.compile('{{unbound view.content.name}}'),
didInsertElement: function(){
this._super();
var view = this;
Ember.run.sync();
Ember.run.next(function(){
$('select').trigger('liszt:updated');
});
}
});
App.ChosenSelectView = Ember.Select.extend({
chosenOptions: {},
contentBinding: "controller.arrangedContent",
selectionBinding: "controllers.selectedClientController.content",
optionLabelPath: "content.name",
optionValuePath: "content.id",
prompt: "Choose a client, if any...",
classNames: ["chosen-menu"],
template: Ember.Handlebars.compile(
'{{#if prompt}}{{unbound prompt}}{{/if}}' +
'{{#each content}}{{view App.UnboundSelectOption contentBinding="this"}}{{/each}}'
),
didInsertElement: function(){
this._super();
Ember.run.sync();
var view = this;
Ember.run.next(function(){
console.log('Hi from ChosenSelectView');
this.$().chosen(view.get('chosenOptions'));
});
},
_closeChosen: function(){
// trigger escape to close chosen
this.$().next('.chzn-container-active').find('input').trigger({type:'keyup', which:27});
},
rerender: function(){
if(this.get('state') == 'inDOM'){
// remove now disconnected html
this.$().next('.chzn-container').remove();
}
this._super();
},
rerenderChosen: function() {
this.$().trigger('liszt:updated');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment