Canceling a custom calendar editor leaves the event title ch


Topic: Canceling a custom calendar editor leaves the event title changed

Elena Costa asked a week ago

We open a custom editor when someone selects a calendar appointment. The title in the calendar changes while the person types in the dialog, even before Save is pressed. Cancel closes the dialog but the changed title remains until we reload the page.

The editor receives the selected appointment and binds its title to an input. We only call the backend from Save, so I expected Cancel to discard the changes. What is the correct way to keep the calendar unchanged while editing?


The input is editing the same object that the calendar displays. Create a separate draft when opening the dialog and bind the form to that draft. Cancel can then close the editor without copying anything back into the event collection.

After a successful Save, replace the matching appointment by its stable ID. A shallow copy is enough for a title field, but nested objects or arrays also need their own copies if the form edits them. Please include the editor's assignment and input binding if you are already using a draft.

https://mdbootstrap.com/docs/angular/plugins/calendar/


Elena Costa commented a week ago

That explains the live update. I copied the reference into draft and bound the input to it. The editor changes only the title and location text; there are no nested fields. Would an object spread be sufficient here?

openEditor(event: Appointment) {
  this.draft = event;
}
// Editor template:
// <input [(ngModel)]="draft.title" />

Yes. Both fields are plain strings, so a shallow object copy separates those edits from the original appointment. Keep the selected ID in the draft and commit its values only after the save request succeeds.

Use the saved appointment returned by your endpoint when available. Closing the dialog through Cancel should simply discard draft; it should not run the collection update below.

openEditor(event: Appointment) {
  this.draft = { ...event };
}
async saveEditor() {
  const saved = await this.api.saveAppointment(this.draft);
  this.events = this.events.map(event =>
    event.id === saved.id ? saved : event
  );
}

Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: MDB5 9.1.0
  • Device: Laptop
  • Browser: Firefox
  • OS: Ubuntu
  • Provided sample code: No
  • Provided link: No