jeudi 21 janvier 2016

Ember updating array does not update template

I'm going mad with this problem: I have a component with a template that uses a {{#each}} loop to iterate an array of comments;

comments: Ember.computed('commentsModel', function() {
    return this.get('commentsModel').toArray();
}),

actions: {
    addOne: function() {
        this.set('comments', [{content: 'agent k'}]);
    }
}

The component is called like this:

{{photo-comments commentsModel=model.comments}}

Where model.comments is returned by the model hook of the route; When I click the button that triggers "addOne" event, the template updates correctly, all the comments are removed and only the new comment "agent k" is shown;

but if I want to ADD a comment into the comments array, instead of overwriting the whole array, it doesn't work; I get no errors but the template simply ignores it and does not updates!

I tried with push:

actions: {
    addOne: function() {
        this.set('comments', this.get('comments').push({content: 'agent k'});
    }
}

with pushObject:

actions: {
    addOne: function() {
        this.set('comments', this.get('comments').pushObject({content: 'agent k'});
    }
}

nothing! Since this is driving me crazy, can someone tell what's wrong with this?




Aucun commentaire:

Enregistrer un commentaire