当使用动态生成的对象数组填充ember-power-select时,无法将对象转换为Array.toString(native)的原始值

时间:2017-07-30 13:54:31

标签: javascript arrays select ember.js

我需要使用商店中的对象填充power-select,但我只能使用硬编码数组myOptions显示选项。我试图动态生成myOption2,并且显然得到与myOptions相同的输出,但它会引发错误:

ember.debug.js:19845 Uncaught TypeError: Cannot convert object to primitive value
at Array.toString (native)
at Object.addListener (http://localhost:4200/assets/vendor.js:30682:88)
at Object.addObserver (http://localhost:4200/assets/vendor.js:34026:23)
at Array.addObserver (http://localhost:4200/assets/vendor.js:47679:27)
at Class.updateOptions (http://localhost:4200/assets/vendor.js:88968:17)
at invoke (http://localhost:4200/assets/vendor.js:11825:16)
at Queue.flush (http://localhost:4200/assets/vendor.js:11891:11)
at DeferredActionQueues.flush (http://localhost:4200/assets/vendor.js:11699:17)
at Backburner.end (http://localhost:4200/assets/vendor.js:11013:25)
at Backburner.run (http://localhost:4200/assets/vendor.js:11135:18)
function addListener(obj, eventName, target, method, once) {
_emberMetalDebug.assert('You must pass at least an object and event name to Ember.addListener', !!obj && !!eventName);
_emberMetalDebug.deprecate('didInitAttrs called in ' + (obj && obj.toString && obj.toString()) + '.', eventName !== 'didInitAttrs', {
  id: 'ember-views.did-init-attrs',
  until: '3.0.0',
  url: 'http://emberjs.com/deprecations/v2.x#toc_ember-component-didinitattrs'
});
//template.hbs

{{#power-select options=myOptions onchange=(action "foo") as |opt|}}
  {{opt.name}}
{{/power-select}}

//component.js

myOptions: [
  { name: 'First Option' },
  { name: 'Second Option' }
],

myOptions2: [],

didReceiveAttrs() {
  this.get('model.content').forEach((option) => {
    this.get('myOptions2').push(option._data);
  });
}


//#=> myOptions: [{name: 'First Option'},{"name":"Second Option"}]
//#=> myOptions2 [{name: 'First Option'},{"name":"Second Option"}]

提前致谢!

1 个答案:

答案 0 :(得分:0)

您需要使用push而不是pushObject,只有观察者才会触发计算属性,模板将自动更新。

this.get('myOptions2').pushObject(option._data)

据说,我不确定这会解决你的问题。