Sha256: e6600cdd9352fb546c950c9cc5ec24b35214addd3660db48ef4aa6d3cad1693c

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

class Backbone.Poised.BaseControl extends Backbone.Poised.View
  template: _.template('<div class="info"><label><%= label %></label><div class="hint"></div><div class="input"></div></div>')

  initialize: (options = {}) =>
    @initOptions   = options
    throw new Error('Missing `model` option') unless options.model?
    throw new Error('Missing `attribute` option') unless options.attribute?

    @attribute = options.attribute

    @label = options.label

    @options = _.omit(options, 'el')

    @visibilityParameters = if @options.visibility
      @options.visibility.slice(0)
    else
      []
    @visibilityCallback = @visibilityParameters.pop()

    for p in @visibilityParameters
      @model.on("change:#{p}", @renderVisibility)

    if @options.validate
      @model.on 'validated', @hintValidation

    @on 'liveChangeStart', =>
      @parentView.trigger('controlLiveChangeStart', this) if @parentView?
    @on 'liveChange', =>
      @parentView.trigger('controlLiveChange', this) if @parentView?
    @on 'liveChangeEnd', =>
      @parentView.trigger('controlLiveChangeEnd', this) if @parentView?

  hintValidation: (isValid, model, errors) =>
    attributeValid = @model.isValid(@attribute)
    if errors isnt @lastErrors
      @$el.toggleClass('invalid', !attributeValid)
      @$hint = @$('.hint')
      @$hint.attr 'data-error': errors[@attribute]
    @lastErrors = errors

  render: =>
    @renderVisibility()
    @$el.attr('class', "poised control #{@attribute}")
    @$el.html @template
      label: @label or @loadLocale "formFields.#{@attribute}.label",
        defaultValue: @attribute.toLabel()
    @$input = @$el.find('.input')
    this

  clone: (options = {}) =>
    new this.__proto__.constructor(_.defaults(options, @initOptions))

  renderVisibility: =>
    return unless @visibilityCallback
    parameters = (@model.get(p) for p in @visibilityParameters)
    isVisible = @visibilityCallback.call(parameters)
    @$el.toggleClass('hidden', !isVisible)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ela-4.1.1 app/js/vendor/backbone.poised/form/base_control.coffee
ela-4.1.0 app/js/vendor/backbone.poised/form/base_control.coffee