Skip to content

Instantly share code, notes, and snippets.

@monzou
Last active December 17, 2015 12:49
Show Gist options
  • Save monzou/5612505 to your computer and use it in GitHub Desktop.
Save monzou/5612505 to your computer and use it in GitHub Desktop.
backbone-forms Select2 editor
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