Last active
December 17, 2015 12:49
-
-
Save monzou/5612505 to your computer and use it in GitHub Desktop.
backbone-forms Select2 editor
This file contains hidden or 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
Backbone.Form.editors.Select2 = class extends Backbone.Form.editors.Select | |
initialize: (options) -> | |
options.options or= [] | |
super options | |
@config = _.extend { placeholder: '', allowClear: true }, @schema.select2 or {} | |
@$el.on "change", (e) => @focus() # retrieve focus ! | |
@$el.on "select2-focus", (e) => @trigger "focus", @ | |
@$el.on "select2-blur", (e) => @trigger "blur", @ | |
render: -> | |
options = @schema.options | |
options.unshift "" if @config.allowClear and (not _.isEmpty options) and (not @$el.prop "multiple") | |
@setOptions options | |
delayed(=> | |
@$el.select2 @config | |
@focusser = @$el.prev().find('.select2-focusser')[0] | |
@$focusser = $(@focusser) | |
) | |
@ | |
focus: -> | |
@$focusser.focus() if @$focusser and (not @$focusser.is ':focus') | |
blur: -> | |
@$focusser.blur() if @$focusser and (@$focusser.is ':focus') | |
select: -> | |
@$focusser.select() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment