How to update nested object in ember.js?

Member

by domenico , in category: Javascript , 4 months ago

How to update nested object in ember.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 4 months ago

@domenico 

To update a nested object in Ember.js, you can follow these steps:

  1. Access the parent object that contains the nested object you want to update. You can do this by using Ember Data to fetch the parent object from the store.
  2. Once you have access to the parent object, you can update the nested object by directly setting its properties. For example, if you have a parent object called 'user' that contains a nested object called 'profile', you can update the nested object like this:
1
2
3
let user = this.store.peekRecord('user', userId);
user.set('profile.firstName', 'John');
user.save();


  1. After updating the nested object, you should save the parent object to persist the changes to the database.
  2. Optionally, you can also use the spread operator to update the nested object in a more concise way:
1
2
3
4
5
6
7
let user = this.store.peekRecord('user', userId);
let updatedProfile = {
  ...user.get('profile'),
  firstName: 'John',
};
user.set('profile', updatedProfile);
user.save();


By following these steps, you can easily update a nested object in Ember.js and ensure that the changes are persisted to the database.