Sha256: af0a57d88333af049046b6a013a709c1a8c118c12a0a31da2eb76c9b6836fd4f

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

classes.DropdownView = Backbone.View.extend
  tagName: "div"
  className: "dropdown"
  backboneClass: "DropdownView"

  initialize: (opts) ->
    if !@collection?
      @collection = new classes.DropdownCollection
    @opts = _.extend({width: "100%", disable_search_threshold: 2}, opts)
    @template = JST["dropdown"];
    @listenTo(@collection, 'add', @render)
    @collection.each (model) =>
      @set_default(model)

  set_default: (model) ->
    model.set('selected', 'selected') if model.get('label') is @opts.default_to

  add: (params) ->
    model = params.model
    model.set 'label', model.get(@opts.label || 'label')
    @collection.add(model)
    @set_default(model)
    @render().el

  render: () ->
    @rendered = true;
    @$el.html(this.template(_.extend(@opts, {models: @collection.toJSON()})))

    this.delegateEvents();
    @$('.dropdown-list').chosen({width: @opts.width, disable_search_threshold: @opts.disable_search_threshold}).change((e,selected) =>
      model = @collection.find (m) ->
        selected.selected is m.get('label')
      this.trigger(app.events.SELECT, model)
    )
    @

  set_selected: (model) ->
    @collection.each (m) ->
      if (model is m)
        m.set('selected', 'selected')
      else
        m.unset('selected')
    @render().el

  get_selected: () ->
    @collection.find (model) => @$('select').val() is model.get('label')

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
atlas_assets-0.4.7 lib/assets/javascripts/views/dropdown_view.coffee
atlas_assets-0.4.6 lib/assets/javascripts/views/dropdown_view.coffee