а

Check attached event


#1

Hi
im using checkEvent method to see if an event is attached; if not use attachEvent.but every time it returns true even the first time, the code is like this:

console.log(‘onEventAdded’, scheduler.checkEvent(‘onEventAdded’)) // ‘onEventAdded’, true
if (!scheduler.checkEvent(‘onEventAdded’)) {
console.log(‘onEventAdded created’)// never get here
scheduler.attachEvent(‘onEventAdded’, (id, e) => {
console.log(‘added’)
eventService.create(e).catch(() => {
e.type = ‘not_saved’;
scheduler.updateView();
dhtmlx.alert(‘Data did not saved!’);
});
});
}

so it is a bug or what? how can i face this issue?
thanks for your support


#2

Hi @hmdkzm,

checkEvent return true if an event has some specified handler(s).

API doesn’t distinguish Sheduler handlers (the used in sources) and handlers that you add in code of your app - that is why attachEvent returns true each time.

That is, either the Sheduler or one of the extensions adds the onEventAdded handler at the loading of the sheduler.

Therefore, checkEvent is not suitable for your purposes.
You need to distinguish whether you add the handler, and not to check if the handler exists at all.

You can create some flag for it, for example:

if(!scheduler._$onEventAddedAttached){
  scheduler._$onEventAddedAttached = scheduler.attachEvent(‘onEventAdded’, (id, e) => {
    ...
  });
}