How to catch the changed object in ember.js observer?

Member

by lily , in category: Javascript , 9 months ago

How to catch the changed object in ember.js observer?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 9 months ago

@lily 

In Ember.js, you can use the @each property to observe changes in an array of objects or a specific property of an object.


For example, if you have an array of objects and you want to observe changes in a specific property of each object, you can define an observer like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import { observer } from '@ember/object';

// Define your array of objects
let items = [
  { name: 'Item 1', price: 10 },
  { name: 'Item 2', price: 20 }
];

// Define an observer to watch for changes in the 'price' property of each object
observer('items.@each.price', function() {
  console.log('Price of an item has changed');
});


In this example, whenever the price property of any object in the items array changes, the observer function will be called. Inside the observer function, you can access the specific object that was changed and take any necessary action based on the change.

Related Threads:

How to detect what changed in array in ember.js?
How to clone an ember.js object?
How to delete a property from an ember.js object?
How to update nested object in ember.js?
How to deep copy a complex object with ember.js?
How to use the Observer design pattern in PHP to handle events?