blue
blue

Reputation: 125

Ember.computed not updating with Ember.Array

I'm currently working on a application that works with with websockets to implement a real-time chat (along with many other things).

However, I have some issues with a Ember.computed member that observes a Ember.NativeArray.

I tried a few things found while googling the issue, but none of them seem to work! My array is a Ember.NativeArray defined in a service like this:

chatMessages: Ember.A()

My computed property, in my component, is defined as such:

messages: Ember.computed('liveSocket.chatMessages', function() {
  return this.get('liveSocket').get('chatMessages').toArray().reverse();
}),

And I set objects to the array in my service as follows:

this.get('chatMessages').set(data.id, null);

If I observe the array directly, nothing changes (I may be confused with that, but isn't that the point of embedding an array in a Ember.Array object?). Now, I can easily watch the array for new elements, by observing the liveSocket.chatMessages.length property. Everything works well and new messages are correctly added. The problem is when I try to remove messages. The websocket server is badly engineered, and changing it is a non-possibility: it doesn't remove the indexes of deleted messages, and returns null objects instead.

So I not only need to watch changes to the array's length, but also to its elements. I tried adding liveSocket.chatMessages.@each to my observed elements list, but that doesn't work either. To sum it up:

// Never updates
Ember.computed('liveSocket.chatMessages', ...)

// Updates only on push/pop (as expected)
Ember.computed('liveSocket.chatMessages.length', ...)

// Still only updates on push/pop
Ember.computed('liveSocket.chatMessages.length', 'liveSocket.chatMessages.@each', ...)

Am I missing something here? Is there a way to observe an array's length and all the elements it contains? Is my approach to this wrong? I am using Ember 2.6 and Ember Data 2.6.1.

Thanks!

Upvotes: 0

Views: 778

Answers (1)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9396

I assume in chatMessages you have objects with property title.

So if you want to trigger every time title of any objects changed, you need to compute on :

Ember.computed('[email protected]', ...);

Upvotes: 2

Related Questions