javascript - ember-power-select 中的自定义选项和第一个选项

标签 javascript ember.js ember-data ember-cli ember-power-select

我正在使用 Ember-power-select-with-create 在我的下拉菜单中进行自定义选择。

这是 .hbs 文件中的电源选择部分。

{{#power-select-multiple-with-create
        options=options //array for prefill values
        selected=offer.offer //is where we save it to (offer is the model and has an array named offer)
        onchange=(action (mut offer.offer)) //default onchange for prefill options
        onfocus=(action "handleFocus")
        onblur=(action "handleBlur")
        oncreate=(action "createOffer" offer.offer) //This calls "createOffer"
        buildSuggestion=suggestion
      as |offer|}}
        {{offer}}
      {{/power-select-multiple-with-create}}

我的 .js 文件中的“createOffer”函数如下所示:
它传递了两个值:selected,即 offer.offer 和 searchText,这是用户尝试添加的输入。

createOffer(selected, searchText)
{
  if (selected == null)
  {
    selected = []; //This is what I'm currently trying: if its null initialize it
  }

  if (selected.includes(searchText)) { //Don't add if it's already added
    this.get('notify').alert("Already added!", { closeAfter: 4000 });
  }
  else { // if it's not yet added push it to the array
    selected.pushObject(searchText);
  }
}

此代码非常适用于添加我们预定义的选项以及自定义选项,但如果它是我们尝试添加到新报价组的第一个自定义报价,则它不起作用。

我假设它与数组尚未初始化这一事实有关。指向这是由于数组未初始化的事实的指针是我收到一条错误消息:无法在未定义的情况下调用 pushObject。我假设它使用预定值的原因是 ember-power-select 可能在某处初始化它,但我找不到任何关于它的文档。

感谢您的回答!

最佳答案

如果有人偶然发现了这个,这就是解决方案
在我们创建报价时调用它,然后像这样初始化报价数组:

offer.set('offer', []);

关于javascript - ember-power-select 中的自定义选项和第一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50413303/

相关文章:

javascript - 使用 % 进行查询(ajax+javascript+html)

javascript - rollupjs 和 sapper - 未定义要求

arrays - Ember.js:使用@each 观察数组属性不起作用

ember.js - 如何仅在模型的路线上时在列表中渲染模型的模板?

javascript - Ember 计算属性是否旨在与异步代码一起使用/包含异步代码?

javascript - 如何从函数中调用/返回ajax成功对象

javascript - 如何过渡到 404 "catch-all"样式路由?

ember.js - 有没有办法在不指定关系名称的情况下获取 Ember 模型的 hasMany 子项?

javascript - 将带有参数的 View 组件渲染到命名导出 ember.js

javascript - 解压错误 [错误 : invalid signature: 0xff000001]