Immutable calendar objects?


#1

Hey there! In integrating Mobiscroll into our product, we’ve notice that the calendar component mutates the passed object if it is missing Mobilscroll’s internal _id attribute: the calendar adds the ID parameter.

Is it possible (nothing in docs) to either:

  • Direct Mobiscroll to not mutate an object?
  • Specify the name (we use id) of the employed attribute?

Mutation of objects in a manner that can propagate backwards through the rest of the application is undesirable behaviour, as would be adding an object attribute for the use of a proprietary presentation plugin.

Immutability is a big part of what we strive for within our product.

Our model instantiation for calendar events appears thus:

export class Event extends Model {
  readonly color: string;
  readonly end: Date;
  readonly id: number;
  readonly start: Date;
  readonly text: string;
  readonly type: EventType;

  constructor(input: Event) {
    super(input);
  }
}
export abstract class Model {
  constructor(input?: object) {
    Object.assign(this, input);
    Object.freeze(this);
  }
}

#2

Hi @d4h,

Thanks for reaching out and welcome to the Mobiscroll Forum!
Indeed, the problem you are facing should not happen. I created a bug report and added you to it, so when it’s fixed, you’ll get a notification. I’ve dug a little deeper into the issue, and it seems the problem is that you are passing an instance of a class as the event. When passing plain objects to the calendar, they will be cloned so that the calendar don’t alters the original object. But in the case of instances of a class they get copied by reference. As a workaround until we fix this, I can recommend to pass plain objects as the events instead of the class instances.

Let me know if that makes sense!

Best,
Zoli


#3

@Zoli Ah brilliant, didn’t realise I hit a bug! Thank you for the prompt response and input. :slight_smile: