1
0
mirror of https://github.com/DataTables/DataTables.git synced 2024-12-10 22:24:10 +01:00
DataTables/media/unit_testing/tests_onhold/4_server-side/fnServerParams.js
Allan Jardine 2b6788011f New: ajax parameter to control all aspects of Ajax that DataTables uses
- DataTables 1.9 had 5 different parameters that controlled how Ajax
  data was obtained, which with its own naming properties, often mapping
  to the jQuery.ajax methods, or otherwise extending them. To hugely
  simply and extend the Ajax functionality DataTables has, these five
  parameters have now been deprecated and the funtionality provided by
  them merged into the new `ajax` parameter.

- Deprecated properties:
   - sAjaxSource
   - fnServerData
   - sAjaxDataProp
   - sServerMethod
   - fnServerParams

- Note that these parameters are still fully supported and can be used,
  but for new projects, `ajax` should be used as they will eventually be
  removed (likely DataTables v2 whenever that is, as they are too widely
  used to be removed in v1.x).

- Added additional / missing tests for the deprecated properties to
  ensure full backwards compatiblity

- The new `ajax` property is fully documented in the doc comments, but
  as a summary it can take three forms:
   - string - the url to get the data from (i.e. this is the new
     sAjaxSource)
   - object - maps directly to jQuery.ajax, allowing full control of the
     Ajax call (provides the abilities of fnServerParams, sServerMethod,
     sAjaxDataProp)
   - function - a function so you can get the data your own way
     (provides the abilities of fnServerData)

- Added unit tests for the new `ajax` property and doc comment examples
  updated to use this property exclusively.
2013-02-10 11:58:58 +00:00

123 lines
2.6 KiB
JavaScript

// DATA_TEMPLATE: empty_table
oTest.fnStart( "fnServerParams" );
$(document).ready( function () {
/* Check the default */
var json = {};
var oTable = $('#example').dataTable( {
"bServerSide": true,
"sAjaxSource": "../data_sources/param.php"
} ).on('xhr', function (e, settings, o) {
json = o;
} );
oTest.fnWaitTest(
"jQuery anti-cache parameter was sent",
null,
function () {
return json.get && json.get._;
}
);
oTest.fnWaitTest(
"Default SSP parameters were sent",
null,
function () {
return 36 === $.map( json.get, function (val) {
return val;
} ).length;
}
);
oTest.fnWaitTest(
"Send additional parameters",
function () {
oSession.fnRestore();
json = {};
$('#example').dataTable( {
"bServerSide": true,
"sAjaxSource": "../data_sources/param.php",
"fnServerParams": function ( data ) {
data.push( { name: 'test', value: 'unit' } );
}
} ).on('xhr', function (e, settings, o) {
json = o;
} );
},
function () {
return json.get && json.get.test === 'unit';
}
);
oTest.fnTest(
"Default parameters were still sent",
null,
function () {
return 37 === $.map( json.get, function (val) {
return val;
} ).length;
}
);
oTest.fnWaitTest(
"Send multiple parameters",
function () {
oSession.fnRestore();
json = {};
$('#example').dataTable( {
"bServerSide": true,
"sAjaxSource": "../data_sources/param.php",
"fnServerParams": function ( data ) {
data.push( { name: 'test', value: 'unit' } );
data.push( { name: 'tapestry', value: 'king' } );
}
} ).on('xhr', function (e, settings, o) {
json = o;
} );
},
function () {
return json.get && json.get.test === 'unit' && json.get.tapestry === 'king';
}
);
oTest.fnWaitTest(
"Delete parameters",
function () {
oSession.fnRestore();
json = {};
$('#example').dataTable( {
"bServerSide": true,
"sAjaxSource": "../data_sources/param.php",
"fnServerParams": function ( data ) {
var remove = function ( a, param ) {
for ( var i=0 ; i<a.length ; i++ ) {
if ( a[i].name === param ) {
a.splice( i, 1 );
return;
}
}
};
remove( data, 'bRegex_0' );
remove( data, 'bRegex_1' );
remove( data, 'bRegex_2' );
remove( data, 'bRegex_3' );
remove( data, 'bRegex_4' );
}
} ).on('xhr', function (e, settings, o) {
json = o;
} );
},
function () {
return json.get && 31 === $.map( json.get, function (val) {
return val;
} ).length;
}
);
oTest.fnComplete();
} );