[英]Cannot convert object to primitive value at Array.toString (native) when populating ember-power-select with array of objects generated dynamically
I need to populate a power-select with objects from the store, but I can only get the options displayed with the hard coded array myOptions
. 我需要用商店中的对象填充电源选择,但是我只能使用硬编码数组
myOptions
显示选项。 I tried to generate myOption2
dynamically, and apparently got the same output as myOptions
, but it throws an error: 我试图动态生成
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"}]
Thanks in advance! 提前致谢!
Instead of push
you need to use pushObject
, only then observer,computed property will fire and templates will be updated automatically. 而不是
push
你需要使用pushObject
,只有观测,计算性能将火和模板将自动更新。
this.get('myOptions2').pushObject(option._data)
Being said that, I am not sure this will fix your issue. 话虽如此,我不确定这是否可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.