Server-side processing with pipelining

By default, this function works with $_GET . Based on in this discussion, I changed this function and got something like below. Now the problem is that Firebug reports an error

json.aaData undefined @line 99

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

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

PHP's counterpart is responding, and this table is 100% working without pipelining. But when I allow the pipeline to get 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?

+4
source share
1 answer

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

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

You need to handle the case when oCache.lastJson not filling (or only partially filling):

  • by initializing oCache.lastJson with default properties
  • by testing for the presence of properties before trying to use them and act accordingly,

For instance:

 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 say if this is 100% correct. You may need to put less inside the if clause, for example, it may be advisable to suppress only two lines starting with json.aaData.splice...

+2
source

Source: https://habr.com/ru/post/1411866/


All Articles