0

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is still relevant in Ember 1.0+: Ember.MutableArray.replace is the Observer-compatible equivalent of JavaScript's Array.splice. As @shex noted, the method signature is slightly different: it accepts an Array of elements to be inserted, rather than accepting a variable length list of parameters.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.