简体   繁体   中英

Server-side processing with pipelining

By default this function works with $_GET . Based on this discussion I modified this function and got something like below. Now the problem is, Firebug gives error message

json.aaData is undefined @ line 99

Here is line (located at the end of code) :

json.aaData.splice( 0, iRequestStart-oCache.iCacheLower ); 

PHP side responds, and this table works 100% without pipelining. But When I enable pipelining getting this error:

http://screencast.com/t/GOJzPHq3kg

function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
    var iPipe = 5; /* Ajust the pipe size */

    var bNeedServer = false;
    var sEcho = fnGetKey(aoData, "sEcho");
    var iRequestStart = fnGetKey(aoData, "iDisplayStart");
    var iRequestLength = fnGetKey(aoData, "iDisplayLength");
    var iRequestEnd = iRequestStart + iRequestLength;
    oCache.iDisplayStart = iRequestStart;

    /* outside pipeline? */
    if ( oCache.iCacheLower < 0 || iRequestStart < oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
    {
        bNeedServer = true;
    }

    /* sorting etc changed? */
    if ( oCache.lastRequest && !bNeedServer )
    {
        for( var i=0, iLen=aoData.length ; i<iLen ; i++ )
        {
            if ( aoData[i].name != "iDisplayStart" && aoData[i].name != "iDisplayLength" && aoData[i].name != "sEcho" )
            {
                if ( aoData[i].value != oCache.lastRequest[i].value )
                {
                    bNeedServer = true;
                    break;
                }
            }
        }
    }

    /* Store the request for checking next time around */
    oCache.lastRequest = aoData.slice();

    if ( bNeedServer )
    {
        if ( iRequestStart < oCache.iCacheLower )
        {
            iRequestStart = iRequestStart - (iRequestLength*(iPipe-1));
            if ( iRequestStart < 0 )
            {
                iRequestStart = 0;
            }
        }

        oCache.iCacheLower = iRequestStart;
        oCache.iCacheUpper = iRequestStart + (iRequestLength * iPipe);
        oCache.iDisplayLength = fnGetKey( aoData, "iDisplayLength" );
        fnSetKey( aoData, "iDisplayStart", iRequestStart );
        fnSetKey( aoData, "iDisplayLength", iRequestLength*iPipe );

        jQuery.post( sSource, aoData, function (data) { 
            /* Callback processing */
            oCache.lastJson = jQuery.extend(true, {}, data);

            if ( oCache.iCacheLower != oCache.iDisplayStart )
            {
                data.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
            }
            data.aaData.splice( oCache.iDisplayLength, data.aaData.length );

            fnCallback(data)
        },"json" );
    }
    else
    {
        json = jQuery.extend(true, {}, oCache.lastJson);
        json.sEcho = sEcho; /* Update the echo for each response */
        json.aaData.splice( 0, iRequestStart-oCache.iCacheLower ); // <- this line
        json.aaData.splice( iRequestLength, json.aaData.length );
        fnCallback(json);
        return;
    }
}

What am I missing? Any suggestion?

json gets its properties, inside fnDataTablesPipeline() , from oCache.lastJson but only if bNeedServer is true.

If fnDataTablesPipeline() has never been called or has been called but bNeedServer remained false, then the line oCache.lastJson = jQuery.extend(true, {}, data); will not have been executed and json.aaData.splice() will fail.

You need to handle the case that oCache.lastJson is not populated (or only partially populated) either:

  • by initialising oCache.lastJson with default properties
  • by testing for the existence of properties before trying to use them and acting accordingly,

For example:

else{
    if(oCache.lastJson.aaData) {
        json = jQuery.extend(true, {}, oCache.lastJson);
        json.sEcho = sEcho; /* Update the echo for each response */
        json.aaData.splice( 0, iRequestStart-oCache.iCacheLower );
        json.aaData.splice( iRequestLength, json.aaData.length );
        fnCallback(json);
    }
}

I can't tell whether this is 100% correct. You may need to put less inside the if clause, for example it may be appropriate to suppress only the two lines starting json.aaData.splice... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM