record.save()

Persists a record's changes to the backend via the adapter.

Since Ember 4/5 (Octane) Spec ↗

Syntax

record.save(): Promise<Model>

Returns

Promise<Model> — Resolves to the saved record.

Throws

  • AdapterError — Rejected with validation or server errors.

Examples

import Component from '@glimmer/component';
import { service } from '@ember/service';
import { action } from '@ember/object';

export default class EditPostComponent extends Component {
  @service store;

  @action
  async save() {
    const post = this.args.post;
    post.title = 'Updated title';
    try {
      await post.save();
    } catch (e) {
      // post.errors holds validation messages
    }
  }
}

Notes

save() issues a POST for new records and a PATCH/PUT for existing ones. On failure the record stays dirty and errors populate record.errors; call rollbackAttributes() to revert local changes.