0
votes

what's the best approach to updating an element in the content property of an ArrayController so that any associated views are updated.

Currently, i use the following piece of code, which updates the content but not the view

    updateContent: function(device) {
        for (var index = 0; index < this.content.length; index++) {
            if (this.content[index]._id === device._id) {
                this.content.splice(index, 1, device);
                break;
            }
        }
    }

What's the best approach for doing this?

In this particular case I am receiving calls from the server, using soket.io, as the state of devices change on updating the view accordingly.

2

2 Answers

3
votes

The method name you search is replace (found in MutableArray, which ArrayProxy inherits from).
Note the API differs in that you pass an array of elements to insert, e.g.

this.content.splice(index, 1, [ device ]);

As @ebryn stated, the Array's splice is not overridden, and the original method does change the array's content, but do not notify the observers, where the view (DOM changes handled by handlebars) is probably one of them.

I've checked this in version 0.96.

1
votes

You're not using observable array methods on the content array, which is why the view isn't being updated.

See the Ember.Array/Ember.Enumerable docs for a list of observable array methods:

http://docs.emberjs.com/#doc=Ember.Array&src=false

http://docs.emberjs.com/#doc=Ember.Enumerable&src=false