1

Let us say i have a model X

  idOfX: DS.attr()
  shouldBeIncluded: DS.attr(),
  type: DS.attr(),
  valueOfX: DS.attr(

And I'm trying to fetch all elements of type x using findAll/peekAll methods of ember store.

After the store method resolves with data, I want to be able to filter data on type attribute or do some operations using valueOfX attribute.

What is the best way to access these values.

Note: Previously I was accessing record attribute of internalModel object in recordArray. But with ember data 2.11.x record attribute is null until i explicitly fetch each record with getRecord method. And I don't know why/how that is happening.

Could anyone suggest share thoughts on it?

I'm using ember-cli@2.10.0, ember@2.10.0

Prabhakar Poudel
  • 339
  • 7
  • 19
  • 1
    Avoid attribute name `type` i guess it's reserved for ember-data. in one of the later release you might get error. to iterate refer this answer http://stackoverflow.com/a/41716142/5771666 – Ember Freak Mar 10 '17 at 08:10
  • I made up this model just for an example sake, let's assume `type` is changed to `isOfType` – Prabhakar Poudel Mar 10 '17 at 08:25
  • ok. then Ebrahim answer is applicable to your question. you can use any of the enumerable methods defined in Ember.ArrayProxy – Ember Freak Mar 10 '17 at 08:28

1 Answers1

2

You shouldn't use internal model.

For accessing the model instances you need to do like :

this.get('store').findAll('x-model').then((dataList) => {
  //deal with dataList like an array
  dataList.forEach((item) => {
     //the item is the exact model instance and 
     //you can access to attributes like belo
     let type = item.get('type');  //for example
  });
});

for peekAll is the same

Ebrahim Pasbani
  • 9,168
  • 2
  • 23
  • 30
  • The `dataList` in this case will be of type `subclass of Ember.ArrayProxy`. I need to get the actual contents of each record to do the operations. So, `dataList` has `get` method calling it with `contents` argument will fetch contents array. Which is an array of internalModel for each record. And the problem comes back to where it started from. – Prabhakar Poudel Mar 10 '17 at 08:28
  • 1
    @yuvrajzohan You can iterate over `dataList` and access to each element. No need to access to `content` directly. I updated my answer to clarify – Ebrahim Pasbani Mar 10 '17 at 08:39
  • This seems to be working fine and as I needed. The documentation in ember data does not clearly specify data accessing. Thanks. – Prabhakar Poudel Mar 10 '17 at 08:59
  • This answer is correct, but you can also just do this `dataList.toArray()` instead of `dataList.forEach(...)`, depending on the use case. – AlexMA Mar 10 '17 at 20:09
  • @alex I ended up using forEach. But is there any benefits leveraged with dataList.toArray() as compared to dataList.forEach(). As I have to iterate the dataList anyway either it's of array class or arrayProxy class. – Prabhakar Poudel Mar 13 '17 at 04:35
  • @yuvrajzohan it's not better for this particular use case, but there are times when forEach isn't needed. I mentioned it for completeness so people don't use forEach just to reimplement toArray. – AlexMA Mar 13 '17 at 12:08
  • @AlexMA Yep. I did, indeed needed toArray in the same app in other part of the code for simplicity. Thanks – Prabhakar Poudel Mar 15 '17 at 04:55