Pemo
Pemo

Reputation: 21

Iterating over DS.hasMany in Ember data

I have a real struggle with Ember.

In my model I have an attribute:

options: DS.hasMany('UserOptions', {async: false})

In a view linked to this model I can easily access this property by e.g.:

{{#each options AS |option|}}
    something....
{{/each}}

and that works like a charm.

However when I try to access this model value in controller with: this.get('model.options') instead of getting a lovely array of payment options, I get an ember model array of objects, and there's no way I can access the actual data.

Do you guys have any idea how do I access this data in controller and process it?

Thanks!

Upvotes: 2

Views: 867

Answers (2)

Pemo
Pemo

Reputation: 21

Below code solved my case:

@get('model.options').toArray().forEach((item) ->
  console.log(item.get('parameter_name')]
)

That's true that console.log(@get('model')) was throwing something strange in console, however when I asked for a specific parameter, it was there!

My problem was that I was trying to print out an entire object instead of a specific value. The values were there, it just didn't print the entire object for a reason.

Upvotes: 0

Daniel
Daniel

Reputation: 18682

this.get('model.options') will give you RSVP.Promise, so you need to work with asynchronous code. Use:

this.get('model.options').then(options => {
    options.forEach(option => {
        // do what you need with option
    })
});

Upvotes: 3

Related Questions