No JSON data in POST

Hi,

I am new to DHTMLX and Javascript so I hope you might be able to help.

I am trying to trigger the saving of an ‘event’ via JSON, however I cannot get the JSON data into the Ajax POST.

I have read a lot on your forum which has lead me to most of this solution but I do not seem to be able to get the last part working.

Thanks for any help,

Added the following to allow serialisation and stringify.


<script src="/scheduler/ext/dhtmlxscheduler_serialize.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="/js/json2.js"></script>

Added the following in init().

scheduler.load("/ListEvents.json","json");

scheduler.attachEvent("onAfterLightbox", function(event_id){
                                                  save(event_id);
                                                  return true;   
                                         });  

Added

function save(event_id){

		  var req = new XMLHttpRequest();  

		  req.open('POST', '/UpdateEvents.json', false);   
	        req.setRequestHeader("Content-Type","application/json");
		  var string = JSON.stringify(scheduler.getEvent(event_id));
		  req.send(string);  
		   		     
		}

You have the wrong event (onAfterLigtbox).
Use onEventSave instead

scheduler.attachEvent("onEventSave", function (id, data, is_new_event) {
     // call save(id) here
     // also this must return true on successful save or false on failure
}

It’s just a matter of wrapping your head around the event model.
All events are listed here docs.dhtmlx.com/doku.php?id=dhtm … ler:events. Look at the “data” parameter above too as this holds the lightbox form data

I’m sure the DHTMLX guys can help further.

Thanks for that.

I was confused by another posting that pointed me to the wrong ‘event’.

I have now got the JSON object in the POST but this had lead to a few other questions.

Output in POST:

{"start_date":"2011-04-29T09:40:00.000Z","end_date":"2011-04-29T10:10:00.000Z","text":"New event","id":1303853418054,"_timed":true,"_sday":4,"_inner":false,"_sorder":0,"_count":1,"rec_pattern":"","rec_type":""}
  1. I was expecting the start_date / end_date to be in the format of the
    scheduler.config.xml_date="%Y-%m-%d %H:%i" I had set - is there any configuration over this format ?

  2. id - is this an internal id ? At present my ‘events’ received from the server do not have an ‘id’. If it is only internal can I set any data to not send it in the PUT ?

  3. _timer / _sday / _inner / _sorder / _count - It is not clear to me what these are / if they are needed ? and if not can they be removed easily ?

Thanks again,

(1)
start_date and end_date are actually the js Date objects, not the strings
You can use scheduler’s formatting ability to convert them to necessary string
docs.dhtmlx.com/doku.php?id=dhtm … te_formats

(2) yep, this id is necessary for the internal operation. Basically if you have some kind of DB you need to have ids to be able to store data back in DB ( to know which DB record need to be updated when some event was updated in the scheduler )

(3) they are related to the rendering of events, and not necessary for data storing ( that data is calculated on the fly , by the scheduler )

and if not can they be removed easily ?
The simplest way to achieve it - construct your own js object with necessary properties only, and send it to the server side

Thanks I will give this a go

Thanks I have move forward but a few other questions - sorry.

  1. On the date/time logic I have changed using the scheduler.date.date_to_str the date/time in the event - and it is output in the JSON correctly - I assume this will not cause any issues to the event logic.

  2. At present I am sending the JSON data on ‘onEventSave’ but reading the documentation I am a little confused which are the correct ‘events’ to be saving on.

For example ‘oneventchanged’ / ‘oneventsave’ - do I need to work on both or just one ? Are there others I should handle ? What about ‘deletion’ ?

  1. Removal of the unnecessary properties - I am not sure how this would be achieved in Javascript.

Thanks again.

(1)
When used against data in onEventSave handler - it is safe to modify data object

(2)
Normally there are 3 events for custom saving logic
onEventCreated
onEventUpdated
onBeforeEventDelete

onEventSave fires only when save button pressed in the lightbox form, but there are other ways to modify event ( dnd for example ) , so onEventChanged is more safe

(3)

delete data.id;

Thanks again.

On 2) onEventChanged does not seem to cover ‘saving’ so I have used onEventChanged / onEventSave / onBeforeEventDelete.

I could not find the syntax of either onEventCreated / onEventUpdated in the documentation.

For the deletion case - it is not clear to me how the DB is mean to know this event is a deletion.

Thanks,

Sorry for misleading info

Check
docs.dhtmlx.com/doku.php?id=dhtm … eventadded
docs.dhtmlx.com/doku.php?id=dhtm … entchanged
docs.dhtmlx.com/doku.php?id=dhtm … ventdelete

onEventChanged does not seem to cover ‘saving’
Saving can trigger oneventadded if it is a new event.

it is not clear to me how the DB is mean to know this event is a deletion
The event handler provides ID of event which is deleted on client side, normally it is expected that DB record has the same ID, so you can sync deleting operation.

Thanks again.

One problem I am having I have added

            scheduler.attachEvent("onEventAdded", function(event_id,event_object){
                                              sendevent(event_id,'/AddChangeEvents.json');
                                              return true;   
                                 }); 
            
            scheduler.attachEvent("onEventChanged", function(event_id,event_object){
                                   sendevent(event_id,'/AddChangeEvents.json');
                                   return true;   
                                  });                    
                                     
            scheduler.attachEvent("onBeforeEventDelete", function(event_id,event_object){
                                   sendevent(event_id,'/DeleteEvent.json');
                                   return true;   
                                 });  

But the display is cleared of the ‘lightbox’ summary if I have the ‘onEventAdded’ section above - if I remove this trigger the box is left displayed but (of course) my sendevent logic is not triggered.

thanks,

I tried adding ‘scheduler.updateEvent’ but still the ‘event’ disappears !!

See viewtopic.php?f=6&t=18514

Can you provide the code, which is attached to onEventAdded handler ?
Most probably it still corrupts original date objects, which result in event disappearing.

Sorry I provided the link to give the solution - that now works.