如何使用 ember-power-select-with-create 添加数据



我试图弄清楚如何在gh自述文件中正确使用ember-power-select-with-create,但我被困在了这个例子上。

下面是一个示例:

{{#power-select-with-create
    options=countries
    selected=selectedCountry
    onchange=(action (mut selectedCountry))
    oncreate=(action "createCountry") as |country term|
}}
  {{country.name}}
{{/power-select-with-create}}

我无法弄清楚应该如何定义控制器操作createCountry。有人可以帮助我理解我应该如何定义createCountry吗?伪代码有效。假设上面的模板在application.hbs模板上,下面的国家/地区数组在控制器application.js

  countries: [
    { name: 'United States',  flagUrl: '/flags/us.svg' },
    { name: 'Spain',          flagUrl: '/flags/es.svg' },
    { name: 'Portugal',       flagUrl: '/flags/pt.svg' },
    { name: 'Russia',         flagUrl: '/flags/ru.svg' },
    { name: 'Latvia',         flagUrl: '/flags/lv.svg' },
    { name: 'Brazil',         flagUrl: '/flags/br.svg' },
    { name: 'United Kingdom', flagUrl: '/flags/gb.svg' },
  ]

您需要创建一个国家/地区对象并将其推送到数组countries可能如下所示;

createCountry(countryName){
    let newCountry = {name: countryName, flagUrl: 'flags/unknown.svg'}; 
    this.get('countries').pushObject(newCountry);
}

您应该考虑为新添加的国家/地区放置一个未知的标志。

最新更新