Ember - 在组件中绑定Ember.select

时间:2014-06-23 05:10:51

标签: ember.js

我继承了一个Ember项目,并一直在学习。几个SO问题涉及Ember。在控制器/视图上下文中选择绑定。我正在组件环境中工作,并且在翻译建议时遇到了问题。帮助

这甚至可能不是我需要的。我的目标是根据另一个选择改变选择菜单,反之亦然。如果用户选择下面的MeasurementType,则应将测量单位(uoms)减少到具有该测量类型的那些列表。如果没有测量类型且用户选择UOM,则测量类型应切换到UOM使用的类型。

车把代码:

{{! ############## }}
{{! # Meas. Type # }}
{{! ############## }}
{{#unless hasMeasurementType}}
    <td class='col-lg-1'><span class="label label-default">{{unbound attribute.measurementType}}</span></td>
{{else}}
    <td class='col-lg-1'>
        {{view Em.Select content=measurementTypes
            optionValuePath="content.name"
            optionLabelPath="content.name"
            value=attribute.measurementType
        }}
    </td>
{{/unless}}

{{! ############## }}
{{! # UOM        # }}
{{! ############## }}
<td class='col-lg-1'>
{{view Em.Select content=uoms
                 optionValuePath="content.name"
                 optionLabelPath="content.name"
                 value=attribute.uom
}}
</td>

咖啡脚本:

App.NumberAttributeComponent = Ember.Component.extend(
  tagName: 'tr'
  attributeBindings: ['data-id']
  precisions: [0, 1, 2, 3, 4, 5, 6]
  'data-id': Ember.computed.oneWay('attribute.id')
  'hasMeasurementType': false
  attributeMeasurementType: ''
  attributeUom: ''

  setHasMeasurementType: (->
    attribute = @.get('attribute')
    @.set('attributeMeasurementType', attribute.get 'measurementType')
    console.log("MT: " + @get 'attributeMeasurementType')
    @.set('hasMeasurementType', Ember.isNone(@get 'attributeMeasurementType'))
  ).on("init")

  setAttributeUom: (->
    attribute = @.get('attribute')
    @.set('attributeUom', attribute.get 'uom')
    console.log("UOM: " + @get 'attributeUom')
  ).on("init")

  setMeasurementTypeLookup: (->
    store = Specx.__container__.lookup('store:main')
    store.find('measurementType').then ((measurementTypes) =>
      @set 'measurementTypes', measurementTypes
    )
  ).on("init")

  getAllUoms: (->
    console.log("Get all UOMs")
    attribute = @.get('attribute')
    store = Specx.__container__.lookup('store:main')
    store.find('uom').then ((uoms) =>
      @set 'uoms', uoms
    )
  )

  getUomsByMeasurementType: (->
    console.log("Get some UOMs")
    attribute = @.get('attribute')
    store = Specx.__container__.lookup('store:main')
    store.find('uom').then ((uoms) =>
      @set 'uoms', uoms.filterBy('measurementType',attribute.get 'measurementType')
    )
  )

  updateUom: (->
    console.log("In updateUom")
    attribute = @get 'attribute'
    if Ember.isNone(@get 'attributeMeasurementType')
      @.getAllUoms()
    else
      @.getUomsByMeasurementType()
  ).observes('attributeMeasurementType', 'attributeUom')

#  didInsertElement: ->
#    console.log("In didInsertElement")
#    attribute = @.get('attribute')
#    @set 'attributeMeasurementType', attribute.get 'measurementType'
#    @set 'attributeUom', attribute.get 'uom'
#    console.log("New: " + attribute.get 'measurementType' + "; " + attribute.get 'uom')
#
#  willDestroyElement: ->
#    console.log("In willDestroyElement")
#    attribute = @.get('attribute')
#    @set 'attributeMeasurementType', attribute.get 'measurementType'
#    @set 'attributeUom', attribute.get 'uom'

  actions:
    edit: ->
      @toggleProperty('isEditing')
    save: ->
      model = @.get('attribute')
      editor = @
      @.set('attributeMeasurementType', model.get 'measurementType')
      @.set('attributeUom', model.get 'uom')
      model.save().then ((specification_attribute) =>
        $.growl.notice title: "Attribute", message: "Attribute saved!"
        editor.toggleProperty('isEditing')
      )
    cancel: ->
      @toggleProperty('isEditing')

0 个答案:

没有答案