panx36
panx36

Reputation: 29

ember map object from ember data store

I am trying to map an object in the model hook. I have this object provided by mirage.

let temp = {"users": [
    {
        "id":1,
        "forename":"Test",
        "surname":"Name"
    },
    {
        "id":2,
        "forename":"Hans",
        "surname":"Solo"
    },
    {
        "id":3,
        "forename":"Peter",
        "surname":"Pan"
    }
]

};

In the model hook I want to use this:

return this.get('store').findAll('user').then(x => x.map(x => x.id + x.forename + x.surname));

I am getting this:

1[object Object][object Object] 2[object Object][object Object] 133000[object Object][object Object] 3[object Object][object Object]

And I am using this in the template (at the moment just for test if it works):

{{#each model as |SingleUser|}}
{{SingleUser}}
{{/each}}

I tried to just map to the ID and this works fine. Furthermore it works fine if I will declare the temp directly in the model hook. Is there anything with promise which impede my map?

I need this working because I am implementing a ember power select multiselection and I want to show the user the id, forename and surename.

Upvotes: 0

Views: 553

Answers (1)

tiunovs
tiunovs

Reputation: 71

Ember wrapping all POJOs in the store with Ember Object. Unlike POJO, Ember Objects have getters/setters and you should use it like:

obj.get('property');
obj.set('property', 'value');

You should change your code to:

.then(x => x.map(x => x.get('id') + x.get('forename') + x.get('surname')));

Here are only a few words about getters and setters in official guide: Ember Guides (at the bottom of the page). You also may read get/set API (URLs to API in the guide article).

Also, take a look at computed properties. Probably, this is will be usable in your case.

Upvotes: 1

Related Questions