Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Lennart on Jan 09, 2008 16:05
open dhtmlx forum
how to keep server-source and dhtmlx-tree synchronous

My question would be: If I use the dataprocessor to automatically update the server's data source, and this update fails for whatever reason, how can I reflect this on client side? I thought to not update the tree (draghandler: return false), sending the data to the server anyway, and use the moveItem() after a successful update of the server's source, but as far as I understand the dataprocessor needs to send the already updated element. Is there a way to reverse the UI-sided update, once the server-sided update has failed? But how do I notice the server's failure?? ;)

Thanks,
Lennart

Answer posted by Support on Jan 10, 2008 03:03
>> Is there a way to reverse the UI-sided update
There is no ready to use functionality, but dataProcessor has a way for handling server side errors and provide some kind of custom reaction.

You can introduce new response types, for example you can add next line to dataprocessor initialization

dp.defineAction("error",my_handler);

on server side, in case of error you can return
    <data>
        <action type="error">
            any info
        </action>
    <data/>

in such case my_handler function will be executed on client side, it will get an action tag object as incoming parameter. Returning false from my_handler will prevent default event processing.

You can also redefine existing event    
    dp.defineAction("update",my_handler);
    function my_handler(xml_node){
        //any custom actions here
        return true;
    }
in such case the my_handler will be called after update operations, returning true from custom handler will cause default update actions in tree.

By using custom event handlers you can return information about server side operation and update UI in necessary way.

Answer posted by Lennart on Jan 10, 2008 09:15
OK, got it, thanks. I have two more questions:

So there is no way to identify a "server lost" or "connection failed" error, right? Uhm, maybe one could use the fact that in dhtmlxcommon.js, starting at line 448, "docObj has no properties"....?

What is the structure of the "xml_node" passed to the custom event handler? From what Firebug tells me it seems to be a quite complicated object? I was wondering if I could use it to identify the affected tree nodes... does it contain their ids or something?
Answer posted by Support on Jan 10, 2008 10:24
>>So there is no way to identify a "server lost" or "connection failed" error, right?
You can do next modification, in dhtmlxdataprocessor.js

    dataProcessor.prototype.afterUpdate = function(that,b,c,d,xml){
        if (that._debug)
            alert("XML status: "+(xml.xmlDoc.responseXML?"correct":"incorrect")+"\nServer response: \n"+xml.xmlDoc.responseText);

        var atag=xml.doXPath("//data/action");

    replace last line with

        var atag=null;

    as result it will block mass update ( which you most probably not using ) , but now you can catch XML error, as any other loading error

function myErrorHandler(type, desc, erData){
    //custom code can be placed here
    return false;
}
dhtmlxError.catchError("LoadXML", myErrorHandler);

The array contains additonal data related to error
    erData=[xmlHttpRequest , object]

First element - xmlHttpRequest object, which contain all data requested
from server.
Second element - dhtmlx object, component which throw error while
loading.

You can get any additional error info by checking xmlHttpRequest
properties (xmlHttpRequest.responseText for example )

>>What is the structure of the "xml_node" passed to the custom event handler?
This is real XMLNode element - just a "action" tag node from XML, you can use
    node.childNodes
    node.data
    or any other DOM methods agains it

If xml is
    <action myid="some"/>
then in you event handler you can use next code to get attribute value
    myid=node.getAttribute("some");

Answer posted on Jan 10, 2008 11:38
>> replace last line with
>> var atag=null;


No, as soon as I do so, I cannot perform any update at all... after
atag=atag.childNodes[i];
atag has the value "\n" and therefor no function
getAttribute(), as called in the next line


>> If xml is <action myid="some"/>
>> then in you event handler you can use next code to get attribute value
>> myid=node.getAttribute("some");



myid=node.getAttribute("myid");
:-)