Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Array observer not working

export default Ember.Object.extend({
    myArray: Ember.A([null,null,null]),
    someFunction: function () {
         console.log('i am triggered');
         myArray[1] = 3;
    }.on('init'),
    hasValue: Ember.observer('myArray.[]', function () {
         console.log('i am changed');
    })
})

What is the mistake here? The observer function never gets triggered though someFunction is always reached.

like image 277
Aditya Kappagantula Avatar asked Sep 04 '26 07:09

Aditya Kappagantula


1 Answers

Use MutableArray#replace:

 this.get('myArray').replace(1, 1, [3]);

This means: starting at index 1, replace 1 element, with a 3. This triggers all the necessary dependencies and observers defined as myArray.[].

I'm surprised that your code does not spit out a ReferenceError, since you are referring to an undeclared variable myArray inside someFunction.

There's a convenience function for this that I can't put my finger on at the moment. Also, be aware that observers may not fire during initialization.