mercredi 6 juillet 2016

How to update nested object in Ember

I'm writing an application using EmberJS 2.4. I have the following service that generates objects of objects of random data:

export default Ember.Service.extend({
    crits: {},
    loadCrits() {
        var newCrits = {};
        var max = Math.random() * 10;
        for(var i=0; i<max; i++) {
            var crit = {};
            var num = Math.random() * 5;
            for(var j=0; j<num; j++) {
                crit["data"+j] = j;
            }

            newCrits["crit"+i] = crit;
        }

        this.set("crits", newCrits);
    }
});

I have a Component that displays the data in a table. For each key-value pair of each nested object, I have a button so that the user can change the value:

<table>
    
    <tr>
        <td>key</td>
        <td><ul>
            
                <li>
                     = 
                    <button >change</button>
                </li>
            
        </ul></td>
    </tr>
    
</table>

The component handles the action thusly:

export default Ember.Component.extend({
    critService: Ember.inject.service(),
    actions: {
        modifyCrit(crit, k) {
            Ember.set(crit, k, "new value");
        }
    }
});

The problem is that the view is not updated. If I understand correctly, this is because Ember doesn't know how to link the "crit" nested object with the "critService" from which is came. There is thus no event/observer triggered that would update the view.

How can I modify this so that when the user clicks the button, the view is updated with the new value?




Aucun commentaire:

Enregistrer un commentaire