Matt
Matt

Reputation: 1811

Ember 2 Actions Inside Each Loop

Consider the following:

days_week: [
    {
        selected '',
        day: 'mon'
    },
    {
        selected '',
        day: 'wed'
    },
    {
        selected '',
        day: 'fri'
    }
]

Inside my template, I can loop through the days:

{{#each days_week as |day_week index|}}
    {{day_week.day}}
{{/each}}

Which produces this: mon wed fri

What I now want to do is assign an action, so that upon click it will add a class, and upon clicking again, it will remove the class....

{{#each days_week as |day_week index|}}
    <button class="{{day_week.selected}}" {{action 'toggle' day_week}}>
        {{day_week.day}}
    </button>
{{/each}}

However, the following code does not seem to work to (start by adding the class highlight)?

actions: {
    toggle: function(day_week){
        day_week.set('selected','highlight');
    }
}

and I get this error: TypeError: day_week.set is not a function ?

Upvotes: 0

Views: 161

Answers (1)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9406

Use Ember.set. It works on any object.

Please check this out

Upvotes: 2

Related Questions