Files
ember-cli-dunlop/app/components/validated-select.coffee
T
2017-12-21 11:42:41 +01:00

38 lines
1.4 KiB
CoffeeScript

import Ember from 'ember'
export default Ember.Component.extend
classNameBindings: ['hasError:error', 'containerClass']
classNames: ['field']
includeBlank: false
disabled: false
valueKey: null # assumed flat array
textKey: 'name'
containerClass: Ember.computed 'attribute', ->
return 'validated-select' unless record = @get('changeset._content')
record_name = record.constructor.modelName || 'record'
"validated-select-#{record_name}-#{Ember.String.dasherize(@get('attribute'))}".replace(/[\W_]/g, '-')
set_value_to_first_option_on_non_blank_list: Ember.observer 'options.length', ->
return unless @get('options.length')
attribute = @get('attribute')
if not @get('includeBlank') and not @get("changeset.#{attribute}")
value = if @get('valueKey') then @get("options.firstObject.#{@get('valueKey')}") else @get('options.firstObject')
@set "changeset.#{attribute}", value
init: ->
@_super()
console.log('No attribute given for validated-select') unless attribute = @get('attribute')
Ember.defineProperty(@, 'hasError', Ember.computed.bool("changeset.error.#{attribute}"))
blankContent: Ember.computed 'includeBlank', ->
setting = @get('includeBlank')
return setting if typeof setting is 'string'
'--'
actions:
hasChanged: (value) ->
[changeset, property] = [@get('changeset'), @get('attribute')]
changeset.set property, value
changeset.validate(property)