Here is my question:
There are two tasks has a link like A -> B , and i set task B a new start_date and new end_date, My function is call back end a API to calculate a new timezone then to set task B new value, when i get the new data from back end and set task B new value(the new value is absolute follow the link’s rule).
But !!! in the event: ‘onAfterAutoSchedule’, I have a function in it, to use the updateIds to getTasks and call API to update tasks data which is been update by AutoSchedule. In here, I use gantt.getTask() function, but now I get pre value of task B !!???
At first, I think maybe is the event: ‘onAfterAutoSchedule’ is called before I get new data form backend and set new data for taskB, but i use debugger findout is not !!! I set new data’s for task B is before the event: ‘onAfterAutoSchedule’ , why in the event, i use gantt.getTask() is get pre value ??
here is my code:
-
get backend’s data to set new data and updateTask()
const newData = rs.content;
if (!!newData && newData.length > 0) {
newData.forEach(i => {
const singleTask = this.gantt.getTask(i.id);
singleTask.start_date = new Date(i.start_date);
singleTask.end_date = new Date(i.end_date);
singleTask.duration = i.duration;
singleTask.workFlow = i.workFlow;
singleTask.principal = i.principal;
singleTask.owningUsers = i.owningUsers;
singleTask.milestone = i.milestone;
singleTask.manHour = i.manHour;
if (this.gantt.getChildren(singleTask.id).length > 0 && !singleTask.milestone) {
singleTask.type = this.gantt.config.types.project;
} else {
singleTask.type = singleTask.milestone ? this.gantt.config.types.milestone : this.gantt.config.types.task;
}
this.gantt.updateTask(i.id, singleTask);
});
} -
use Ids in that event,to call gantt.getTask()
this.gantt.attachEvent(‘onAfterAutoSchedule’, (taskId, updatedTasks) => {
console.log(‘taskId: %o, updatedTasks: %o’, taskId, updatedTasks);
if (taskId) {
updatedTasks = […updatedTasks];
}
const dates = this.extractDates(updatedTasks);
if (dates.length > 0) {
this.updateTaskDates(this.project.id, dates);
}
return true;
}, null);
}
private extractDates(taskIds): any {
const result: {
taskId: string | number,
startDate: Date,
finishDate: Date,
duration: string | number,
constraintType: string,
constraintDate: Date
}[] = taskIds.map(item => {
const task = this.gantt.getTask(item); (problem in here, get pre value)
let constraintType = this.constraintTypeForSelect.find(item => item.id === task.constraint_type)?.key;
if (!constraintType) {
constraintType = this.constraintTypeForSelect.find(item => item.id === this.gantt.config.constraint_types.ASAP)?.key;
}
const children = this.gantt.getChildren(task.id);
if (children.length > 0) {
return {
taskId: task.id,
startDate: this.gantt.getSubtaskDates(task.id).start_date,
finishDate: this.gantt.getSubtaskDates(task.id).end_date,
duration: this.gantt.getSubtaskDuration(task.id),
constraintType,
constraintDate: task.constraint_date
};
} else {
return {
taskId: task.id,
startDate: task.start_date,
finishDate: task.end_date,
duration: task.duration,
constraintType,
constraintDate: task.constraint_date
};
}
});
return result;
}