Reputation: 2837
I'm using the Reactive Array package in Meteor and cannot figure out how to change a value in the array and have the helper rerun. This is such a basic thing, I must be missing something obvious but I've not been able to find an answer. Here's my code:
On the client:
test_arr = new ReactiveArray([3]);
Helper:
UI.registerHelper('array_test', function(){
return test_arr.list();
});
In a template:
{{array_test}}
On screen I see '3' as expected, but if I change the value of the reactive array with this:
test_arr[0] = 4
nothing changes on screen, even though if I run test_arr.list() in the console, I see [4]
. If I push a new value with:
test_arr.push(5)
then the helper reruns and I see 4,5
on the screen, correctly. So the value had been changed, but the helper did not rerun until I performed an unrelated 'push' operation.
I can't see anything in the docs about updating a value, only adding and removing values.
Is there any way to update a value in a reactive array, reactively?
Upvotes: 2
Views: 987
Reputation: 16478
You can use ReactiveArray.splice()
to replace elements in a reactive array, or even create a convenience method that deals with a single element:
ReactiveArray.prototype.setAt = function(pos, item) {
return this.splice(pos, 1, item);
};
arr = new ReactiveArray(['a', 'b', 'c']);
// ["a", "b", "c"]
arr[0];
// "a"
arr.setAt(0, "newVal");
// ["a"]
arr[0];
// "newVal"
Upvotes: 1