首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以将多个参数传递到Ember Power Select中的onChange操作?

是否可以将多个参数传递到Ember Power Select中的onChange操作?
EN

Stack Overflow用户
提问于 2019-05-22 17:34:18
回答 2查看 856关注 0票数 2

作为ember-bootstrap form的一部分,我目前正在使用优秀的ember-power-select插件。

我在表单上有多个下拉项,我正在尝试将它们统一到一个函数中,该函数可以用作电源选择调用中的onChange操作:

代码语言:javascript
复制
{{#form.element 
    controlType="power-select" 
    label="Destination" 
    value=destinationSelection
    options=destinationOptions
    as |el|}}
    {{#el.control   
        onChange=(action "setDropDown")
        searchField="name"  
        as |item|}}
        {{item.name}}
    {{/el.control}}
{{/form.element}}

我的处理函数将根据下拉列表的选择简单地设置一些值:

代码语言:javascript
复制
actions: {
     setDropDown(selected, string) {
      handleDropDown(selected, dropdown, this)
    }
}

function handleDropDown(selected, dropdown, controller) {
  let selection = `${dropdown}Selection`
  let modelid = `model.${dropdown}_id`

  set(controller, selection, selected)
  set(controller, modelid, selected.id)

}

为了让它工作,我真的需要能够从组件调用的onChange部分向setDropDown操作传递一个字符串,否则我无法告诉处理程序函数它应该设置哪些特定字段,而不是为每个dropdown创建一个操作。

但是,当我尝试传递多个参数时,比如

代码语言:javascript
复制
onChange=(action "setDropDown" "destination") 

代码语言:javascript
复制
onChange=(action "setDropDown" selected "destination")

我失去了将所选项目作为第一个参数的onChange操作的基本功能。

我浏览了文档,找不到库作者将多个参数传递给onChange操作的任何示例,我想知道是否可以在不破坏库功能的情况下实现这一点。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-22 19:40:17

您可以使用专门的高阶帮助器函数为ember-power-select创建一个操作,该操作最终将使用额外的参数调用您的操作。考虑这个帮助器handle-dropdown

代码语言:javascript
复制
import { helper } from '@ember/component/helper';

export function invokeFunction([prop, action]) {
    return function(){
        action(prop, ...arguments);
    }
}

export default helper(invokeFunction);

因此,我们在这里要做的是创建将由ember-power-select调用的函数。在此函数中,我们首先使用prop调用原始操作,然后是ember-power-select调用onchange函数时使用的每个参数。

在模板中,在将操作传递给power-select时调用此帮助器

代码语言:javascript
复制
{{#power-select 
   onchange=(handle-dropdown 'foo' (action 'dropdownChanged')) 
   as |dropdown|}}

然后你的行动就会是

代码语言:javascript
复制
actions: {
  dropdownChanged(keyToSet, selectedValue){
    this.set(keyToSet, selectedValue);
  }
}

这最终将调用dropdownChanged('foo', /* the selected value */)

票数 2
EN

Stack Overflow用户

发布于 2019-05-22 19:17:21

对于这样的用例,Ember Bootstrap's Power Select integration为您提供了一个很好的应用程序接口。让我给你举个例子。

让我们以国家/地区选择器为例。我们有一个由对象列表表示的国家列表,这些对象包含由ISO3166-1定义为id属性和名称为name的两个字母的国家代码。选定的国家应该在模型上表示,这是一个POJO由那里的国家代码。

代码语言:javascript
复制
export default Component.extend({
  // country code of country selected or null
  selectedCountry: null,

  // Using a computed property here to ensure that the array
  // isn't shared among different instances of the compontent.
  // This isn't needed anymore if using native classes and
  // class fields.
  countries: computed(() => {
    return [
      { id: 'us', name: 'United States of America' },
      { id: 'ca', name: 'Canada' },
    ];
  }),

  // Using a computed property with getter and setter to map
  // country code to an object in countries array.
  selectedCountryObject: computed('selectedCountry', {
    get() {
      return this.countries.find((_) => _.id === this.selectedCountry);
    },
    set(key, value) {
      this.set('selectedCountry', value.id);
      return value;
    }
  }),
});

现在我们可以像预期的那样使用Ember Bootstrap Power Select:

代码语言:javascript
复制
{{#bs-form model=this as |form|}}  
  {{form.element controlType="power-select" property="selectedCountryObject" label="Country" options=this.countries}}
{{/bs-form}}

免责声明:我自己没有测试过这段代码,所以可能会有打字错误,但我希望你能理解。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56253685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档