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

Member

by lily , in category: Javascript , a month ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , a month 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('[email protected]', 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.