Hello Bryntum Team,
I recently encountered a behavior where default values defined in a custom EventModel are not applied when calling eventStore.add({}).
I am using the project’s CrudManager to load and sync data and have configured a custom EventStore with a subclassed CommonEventModel. The model defines a default value for the name field:
export class CommonEventModel extends EventModel {
static get fields() {
return [
{ name: 'name', type: 'string', defaultValue: 'TEST DEFAULT VALUE' },
];
}
}
Here is my custom event store setup:
class CustomEventStore extends EventStore {
static $name = 'CommonEventStore';
static modelClass: CommonEventModel;
}
const commonEventStore = new CustomEventStore();
console.log(' commonEventStore', commonEventStore.add({})); // name is undefined
Calling eventStore.add({}) does not apply the defaultValue defined in the model. The name field remains undefined.
Configuration Overview:
export const schedulerProProps: BryntumSchedulerProProps = {
project: {
autoLoad: true,
autoSync: true,
transport: {
load: { url: 'http://localhost:3001/api/scheduler/load' },
sync: { url: 'http://localhost:3001/api/scheduler/sync' }
},
eventStore: {
modelClass: CommonEventModel
}
}
// eventStore: commonEventStore // Also tested manually
};
The component is rendered using the React wrapper:
<BryntumSchedulerPro
ref={schedulerRef}
onBeforeEventAdd={({ eventRecord }) => {
console.log('onBeforeEventAdd', eventRecord); // name is still undefined
}}
onBeforeTaskEdit={({ taskElement, taskRecord }) => {
if (taskRecord?.isPhantom && taskRecord?.hasGeneratedId) {
console.log('taskRecord', taskRecord);
// taskRecord.name is set to the Scheduler's default "New Event" instead of using the defaultValue from CommonEventModel ("TEST DEFAULT VALUE").
// Also, taskRecord is an of generatedModelClass(...) rather than generatedCommonEventModel(...),
// unlike when creating a new event via `new CommonEventModel({})`.
navigate(`/event/${taskRecord.id}/create`);
} else {
navigate(`/event/${taskRecord.id}/edit`);
}
return false;
}}
{...schedulerProProps}
/>
I was able to reproduce the same issue using your Planned vs Actual example:
https://bryntum.com/products/schedulerpro/examples/planned-vs-actual/
Modified Task model class:
class Task extends EventModel {
static fields = [
{ name: 'name', type: 'string', defaultValue: 'TEST VALUE' },
{ name: 'durationUnit', defaultValue: 'h' },
{ name: 'plannedStartDate', type: 'date' },
{ name: 'plannedEndDate', type: 'date' },
{ name: 'actualStartDate', type: 'date' },
{ name: 'actualEndDate', type: 'date' },
{ name: 'startDate', dataSource: 'actualStartDate' },
{ name: 'endDate', dataSource: 'actualEndDate' }
];
}
After calling eventStore.add({}), the default value is still not applied.
Is this the expected behavior when using eventStore.add({}) via the project configuration and a custom model class?
If so, is there a recommended way to ensure default values are applied when using the CrudManager and Bryntum Scheduler React wrapper?
Thank you in advance!
Best regards,
Malick