簡體   English   中英

ember-power-select中的自定義和第一選項

[英]Custom and first options in 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', []);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM