Hi I started using Dhtmlx scheduler and it is really cool. I am checking for empty event name or event name already saved in a scheduler object. I am throwing alert based on this check and for the first time it is good. When i close the scheduler and try to add events with same name, the alert is shown multiple times. On tracking the issue, i noticed that "scheduler.attachEvent("onEventSave")" is getting fired multiple times and hence the alert is shown multiple times. How to stop this multiple occurrences on saving a single event.
My code:
scheduler.attachEvent("onEventSave",function(id, ev, is_new){
var event_name_array = [];
var events_JSON_Array = JSON.parse(scheduler.toJSON());
for(var i = 0; i < events_JSON_Array.length; i++)
{
var event = events_JSON_Array[i];
event_name_array.push(scheduler.getEvent(event.id).text);
}
if (!ev.text || (event_name_array.indexOf(ev.text) > -1) && (event_name_array.length > 1)) {
alert('Bad input');
return false;
}
return true;
})
Thanks in advance!!!
Most probably this handler is attached several times. Try adding checking like following
if(!scheduler._onEventSave){
scheduler._onEventSave = scheduler.attachEvent("onEventSave",function(id, ev, is_new){
var event_name_array = [];
var events_JSON_Array = JSON.parse(scheduler.toJSON());
for(var i = 0; i < events_JSON_Array.length; i++)
{
var event = events_JSON_Array[i];
event_name_array.push(scheduler.getEvent(event.id).text);
}
if (!ev.text || (event_name_array.indexOf(ev.text) > -1) && (event_name_array.length > 1)) {
alert('Bad input');
return false;
}
return true;
});
}
If you have several events, you may need to do the same check for all of them. In that case it would be simpler to put all attachEvent in some function and ensure it called only once, e.g.
// define handlers here
scheduler.initAppEvents = function(){
scheduler.attachEvent("onEventSave",function(id, ev, is_new){
....
});
....
// replace self with an empty function after first call
scheduler.initAppEvents = function(){};
};
and call on initialization
scheduler.initAppEvents();