...

/

Save and Cancel Buttons

Save and Cancel Buttons

In this lesson, we’ll discuss the changes in the “Save” and “Cancel” buttons’ functionalities.

Setting the confirmedLeave property

We should also take care to allow clicking the “Save” button. However, we need to add the confirmation dialog when “Save” is clicked, because it triggers a router transition. Simply setting the confirmedLeave property will do:

Press + to interact
// app/controllers/bands/new.js
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
export default class BandsNewController extends Controller {
@service catalog;
@service router;
@tracked name;
constructor() {
super(...arguments);
this.router.on('routeWillChange', (transition) => {
if (this.confirmedLeave) {
return;
}
if (transition.from.name === 'bands.new') {
if (this.name) {
let leave = window.confirm('You have unsaved changes. Are you sure?');
if (leave) {
this.confirmedLeave = true;
} else {
transition.abort();
}
}
}
});
}
@action
updateName(event) {
this.name = event.target.value;
}
@action
async saveBand() {
let band = await this.catalog.create('band', { name: this.name });
this.confirmedLeave = true;
this.router.transitionTo('bands.band.songs', band.id);
}
}

Removing the confirm dialog

Now, it’s the “Cancel” ...

Access this course and 1400+ top-rated courses and projects.