Save multiple events

I’m trying to edit the behaviuor of the scheduler to make it save three events per day when the user clicks the save button of the lightbox (month view).

This is the code snippet for this function

scheduler.attachEvent("onEventSave", function(id,ev){
		var inizioPausa = scheduler.date.add(ev.start_date, 4, 'hour');
		var finePausa = scheduler.date.add(ev.start_date, 5, 'hour');	
	       	scheduler.addEvent({
	    	    start_date: ev.start_date,
	    	    end_date: inizioPausa,
	    	    text:   " ",
	    	    userId: ev.userId,
	    	    type:   "presenza"
	    	}); 
	    	
    		scheduler.addEvent({
	    	    start_date: inizioPausa,
	    	    end_date: finePausa,
	    	    text:   " ",
	    	    userId: ev.userId,
	    	    type:   "pausa"
	    	})
    
    		scheduler.addEvent({
	    	    start_date:  finePausa,
	    	    end_date: ev.end_date,
	    	    text:   " ",
	    	    userId: ev.userId,
	    	    type:   "pausa"
	    	})
	});

The problem is that the events created by this function look good in the scheduler, but not so good in the database: some records are doubled. Moreover I get an IllegalStateException in the consolle and I’m not sure what causes that.
I’m using a Java backend, with a servlet connector.java (see below)


package tts.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.annotation.WebServlet;
import com.dhtmlx.connector.*;

@WebServlet(name="Connector", urlPatterns={"/connector.action"})
public class Connector extends ConnectorServlet {

	private static final long serialVersionUID = -8819552090653981282L;

	@Override
	protected void configure() {
		//obtain DB connection
		Connection conn=null;
		try {
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection("jdbc:mysql://localhost/youtime", "root", "root");
		} catch (Throwable e) {
			e.printStackTrace();
		}

		//Initializes connector
		SchedulerConnector c = new SchedulerConnector(conn);
		//configures the used table and fields
		c.render_table("events","id","start_date,end_date,text,userId,type");
	}
}

Any idea on where is the problem? Any suggestion for implementing this functionality (save multiple custom events in a single day)?
Thanks!!!

Hello,
‘onEventSave’ triggers after scheduler collects form data and before it applies changes.
If you do not cancel default behavior of the lightbox, it will create events that you manually add via addEvent plus the one that has been opened initially(created on double click)

So you need to prevent default processing of the lightbox - return ‘false’ from the handler, and hide lightbox manually.

Maybe the issue happens because dataprocessor sends multiple ajax request when you add events. The possible solution would be disable auto-sending requests and trigger update manually, so all events will be sent in a single request.
Try this code

[code]scheduler.attachEvent(“onEventSave”, function(id,ev){

//dp - instance of dataProcessor
//disable instant sending changes to the server
dp.autoUpdate = false;

var inizioPausa = scheduler.date.add(ev.start_date, 4, 'hour');
var finePausa = scheduler.date.add(ev.start_date, 5, 'hour');
scheduler.addEvent({
	start_date: ev.start_date,
	end_date: inizioPausa,
	text:   " ",
	userId: ev.userId,
	type:   "presenza"
});

scheduler.addEvent({
	start_date: inizioPausa,
	end_date: finePausa,
	text:   " ",
	userId: ev.userId,
	type:   "pausa"
});

scheduler.addEvent({
	start_date:  finePausa,
	end_date: ev.end_date,
	text:   " ",
	userId: ev.userId,
	type:   "pausa"
});


//manually close lightbox
scheduler.endLightbox(false);
scheduler.hide_lightbox();

//send ajax update to the server
dp.sendData();
//re-enable initial update mode
dp.autoUpdate = true;
//return false to prevent default processing
return false;

});[/code]

THANK YOU SO MUCH!!!
That solved the issue! I’ll offer you a coffee when you come to Rome! :smiley: