samedi 1 août 2015

Passing argument on action in Ember

I have three toggle buttons in my "hbs" file. And I have "options" array in the controller related to this template. I want to update "options" whenever user selected/deselected any of buttons. For example, if button 1 selected and other not - "options" must be [1]. If second and third buttons selected and first not - "options" must be [2, 3].

And I tried to make this through actions with parameters:

<button {{action 'toggleOption' name aria-pressed}}
id="first-button" name="1" type="button" class="btn 
option-toggle-button" data-toggle="button" aria-pressed="false"
autocomplete="off">First button</button>

Controller:

import Ember from 'ember';

export default Ember.Controller.extend({
    options: [],
    actions: {
        toggleOption(id, selected) {
            var options = this.get("options");

            if (selected) {
                if (options.contains(id))
                    options.push(id);
            } else {
                var index = options.indexOf(id);
                if (index >= 0)
                    options.splice(index, 1);
            }

            this.set("options", options);
        }
    }
});

But "toggleOption" was calling with "undefined" params so I assume that I on a wrong way.

Question: how can I implement needed logic? Maybe I need a completely different approach to solve this problem?




Aucun commentaire:

Enregistrer un commentaire