最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to change value with Ember.js Array forEach? - Stack Overflow

programmeradmin1浏览0评论
 self.resultList.forEach(function(item, index, enumerable){
                         console.log(self.resultList);
                         item.id=11;
                         item.get('id');
                     });

the item like this:

if item.id = 11; the exception like this:

Assertion failed: You must use Ember.set() to access this property (of [object Object])

so item.get('id') or item.set('id',11)

the exception like this

Uncaught TypeError: Object # has no method 'get'

is this item not the Ember's Object?so what the item is?

could someone tell me how to change the 'itme.id's value..

Thanks a million

 self.resultList.forEach(function(item, index, enumerable){
                         console.log(self.resultList);
                         item.id=11;
                         item.get('id');
                     });

the item like this:

if item.id = 11; the exception like this:

Assertion failed: You must use Ember.set() to access this property (of [object Object])

so item.get('id') or item.set('id',11)

the exception like this

Uncaught TypeError: Object # has no method 'get'

is this item not the Ember's Object?so what the item is?

could someone tell me how to change the 'itme.id's value..

Thanks a million

Share Improve this question edited Dec 24, 2016 at 14:00 Marcio Junior 19.1k4 gold badges46 silver badges47 bronze badges asked Jul 31, 2013 at 16:52 seagodseagod 1171 gold badge1 silver badge4 bronze badges 1
  • 1 What is inside self.resultList? I think the array contains both plain JS Objects and Ember Objects. – mavilein Commented Jul 31, 2013 at 17:16
Add a comment  | 

2 Answers 2

Reset to default 21

You can use the Ember.set(yourObject, propertyName, value); and Ember.get(yourObject, propertyName); to safely set and get properties.

In your case:

self.resultList.forEach(function(item, index, enumerable) {
    Ember.set(item, "id", 11); 
    Ember.get(item, "id");
});

In my case I did it in this way

//In my controller I've defined the array
displayInfoCheckboxes: [
    {
      turnover: {
        label: "Turnover",
        value: 1,
        propertyName: "turnover"
      }
    }, {
      pl: {
        label: "P&L",
        value: 1
      }
    }
]

//and in my handler I passed the full string path to the property in the set method

let displayInfoCheckboxes = this.get('displayInfoCheckboxes');
let controller = this;

displayInfoCheckboxes.forEach(function(items,index) {
  for (var key in items) {
    controller.set('displayInfoCheckboxes.' + index + '.' + key + '.value', false);
  }
})

发布评论

评论列表(0)

  1. 暂无评论