search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Adding a new event with client-side immediate update

Asked by Jeff Kurzner
4 years ago.

I have been able to use the examples to create a new event using the backend create sample. Is there a way to use the message that is returned to immediately update the schedule without having to reload all the events?

I am creating the new event using the click on a new range code sample which calls the newcalevent modal form and then a backend create page.

dp.onTimeRangeSelected = function (args) {
	var modal = new DayPilot.Modal();
	modal.onClosed = function(args) {
		dp.clearSelection();
		var data = args.result;
		if (data && data.result === "OK") { 
			loadEvents(); 
			dp.message(data.message); 
		}
	};
	modal.showUrl("newcalevent.php?start=" + args. start + "&end=" + args.end + "&resource=" + args.resource + "&userid=" + '<?php echo $UserID;?>');
};
Answer posted by Dan Letecky [DayPilot]
4 years ago.

After you create the new event in newcalevent.php, you need to send the event details (including the id) back to the main page using DayPilot.Modal.close():

var eventdata = {
  id: ...,
  start: ...
  // ...
};
DayPilot.Modal.close({event: eventdata, ...});

This object will be available in onClosed as args.result.event so you can use it to create the new event:

modal.onClosed = function(args) {
  dp.clearSelection();
  var data = args.result;
  if (data && data.result === "OK") { 
    dp.events.add(new DayPilot.Event(data.event));
    dp.message(data.message); 
  }
};
Comment posted by Jeff Kurzner
4 years ago.

I was using the following from another sample in my newcalevent to get that information

        $("#f").submit(function (ev) {
            // make sure it's not submitted using the default mechanism
            ev.preventDefault();
            
            // normalize the date values
            $("#start").val(startPicker.date.toString("yyyy-MM-dd"));
            $("#end").val(endPicker.date.toString("yyyy-MM-dd"));
            
            // submit using AJAX
            var f = $("#f");
		    $.post("backend_create.php", f.serialize(), function (result) {
                close(eval(result));
            });
            
        });
Comment posted by Jeff Kurzner
4 years ago.

The code you provided is being used as such:

dp.onTimeRangeSelected = function (args) {
	var modal = new DayPilot.Modal();
	modal.onClosed = function(args) {
		dp.clearSelection();
//		alert('returned data id:' + args.result.id + ' Start=' + args.result.start );
		var data = args.result;
		if (data && data.result === "OK") { 
			dp.events.add(new DayPilot.Event(data.event));
			dp.message(data.message); 
		}
	};
	modal.showUrl("newcalevent.php?start=" + args. start + "&end=" + args.end + "&resource=" + args.resource + "&userid=" + '<?php echo $UserID;?>');
};

HOWEVER, despite seeing the data id being returned and the message being shown on the screen after the add, the new item does not show on the calendar.

Comment posted by Dan Letecky [DayPilot]
4 years ago.

You should modify "backend_create.php" to return the full event data object (including start, end, etc.) as "event" property in the return JSON. Because that's what is passed back to onClosed:

$.post("backend_create.php", f.serialize(), function (result) {  
  // result is the backend_create.php return value
  close(eval(result));
});
Answer posted by Jeff Kurzner
4 years ago.

Exactly my problem was that I incorrectly assumed the submitted data was being passed all the way through the process from the modal back to the originating page.

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.