Forked from andriybuday/input-chosen-ember-component.js
Last active
August 29, 2015 13:58
-
-
Save mattahorton/10068834 to your computer and use it in GitHub Desktop.
A single-select version of andriybuday's multiple-select Ember Chosen.js component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.InputChosenComponent = Ember.Component.extend( | |
init: -> | |
@_super() | |
return | |
# view properties | |
labelClasses: (-> | |
@get("labelClass") + " control-label" | |
).property("labelClass") | |
inputContainerClasses: (-> | |
@get("inputContainerClass") + " input-group" | |
).property("inputContainerClass") | |
inputClasses: (-> | |
@get("inputClass") + " form-control chosen-select" | |
).property("inputClass") | |
disabledFixer: (-> | |
chosenSelect = @$(".chosen-select") | |
chosenSelect.prop("disabled", @get("intputDisabled")).trigger "chosen:updated" | |
chosenSelect.trigger "chosen:updated" | |
return | |
).observes("intputDisabled") | |
# dom stuff | |
setup: (-> | |
chosenSelect = $(".chosen-select") | |
chosenSelect.find("script").remove() | |
chosenSelect.chosen() | |
# styling setup | |
chosenSelect.attr "data-placeholder", @get("dataPlaceholder") | |
$(".chosen-choices").addClass @get("inputContainerClass") | |
$(".search-field > input").addClass "form-control" | |
# initial values | |
optionValueName = @get("optionValuePath").replace("content.", "") | |
currentSelection = @get("chosenSelection").get optionValueName | |
chosenSelect.val currentSelection | |
chosenSelect.prop("disabled", @get("intputDisabled")).trigger "chosen:updated" | |
chosenSelect.trigger "chosen:updated" | |
# handling changes | |
self = this | |
chosenSelect.chosen().change (event, item) -> | |
if item.selected | |
val = parseInt(item.selected) | |
selectedItem = self.get("content").findBy(optionValueName, val) | |
self.set "chosenSelection", selectedItem | |
return | |
return | |
).on("didInsertElement") | |
teardown: (-> | |
).on("willDestroyElement") | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="form-group"> | |
{{input-chosen label="My Team" multiple="true" labelClass="col-sm-1" inputContainerClass="col-sm-5" | |
contentBinding="allPlayers" chosenSelectionBinding="myTeamPlayers" | |
optionLabelPath="content.fullName" optionValuePath="content.playerId | |
dataPlaceholder="Choose team..."}} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment