1
0
mirror of https://github.com/DataTables/DataTables.git synced 2025-02-27 00:54:15 +01:00

Fix: Arrays in settings object shared between instances

- Because of the way _fnExtend() was deep copying objects, but shallow
  copying (i.e. references) arrays, the arrays used in the settings
  object were actually being shared between all instances of DataTables
  on a page.

- This is most noticable in the column filtering, whereby if you apply
  a filter to the column of one table, it is applied to all tables!

- The fix is to dump _fnExtend and replace it with a typical jQuery
  extend. However, one special consideration is made for the data being
  passed it - we absolutely do want that reference to be retained (that
  + the fact that extend is slow on large arrays/objects) so it is
  dumped into a temp variable which is then assigned back to the cloned
  object.

- This fixed DataTables/DataTables issue #213
This commit is contained in:
Allan Jardine 2013-08-05 09:04:23 +01:00
parent a242a848d5
commit 38e12beac9
2 changed files with 9 additions and 43 deletions

View File

@ -1 +1 @@
bf16c47fd69b5d4460b5fa23d97ed1cb1060e4a3
e171e7b3e698822231f23b73dec77fea2320a227

View File

@ -4423,46 +4423,6 @@
}
/**
* Extend objects - very similar to jQuery.extend, but deep copy objects, and shallow
* copy arrays. The reason we need to do this, is that we don't want to deep copy array
* init values (such as aaSorting) since the dev wouldn't be able to override them, but
* we do want to deep copy objects.
* @param {object} oOut Object to extend
* @param {object} oExtender Object from which the properties will be applied to oOut
* @returns {object} oOut Reference, just for convenience - oOut === the return.
* @memberof DataTable#oApi
* @todo This doesn't take account of arrays inside the deep copied objects.
*/
function _fnExtend( oOut, oExtender )
{
var val;
for ( var prop in oExtender )
{
if ( oExtender.hasOwnProperty(prop) )
{
val = oExtender[prop];
if ( $.isPlainObject( val ) )
{
if ( ! oOut[prop] )
{
oOut[prop] = {};
}
$.extend( true, oOut[prop], val );
}
else
{
oOut[prop] = val;
}
}
}
return oOut;
}
/**
* Bind an event handers to allow a click or return key to activate the callback.
* This is good for accessibility since a return on the keyboard will have the
@ -5432,7 +5392,6 @@
"_fnSetObjectDataFn": _fnSetObjectDataFn,
"_fnApplyColumnDefs": _fnApplyColumnDefs,
"_fnBindAction": _fnBindAction,
"_fnExtend": _fnExtend,
"_fnCallbackReg": _fnCallbackReg,
"_fnCallbackFire": _fnCallbackFire,
"_fnNodeToColumnIndex": _fnNodeToColumnIndex,
@ -5557,7 +5516,14 @@
oInit.aLengthMenu[0][0] : oInit.aLengthMenu[0];
}
oInit = _fnExtend( $.extend(true, {}, defaults), oInit );
// Apply the defaults and init options to make a single init object will all
// options defined from defaults and instance options. Note that the aaData
// option is removed then re-added to ensure the original data reference is
// retained. Even if undefined the code below is safe
var tmpData = oInit.aaData;
delete oInit.aaData;
oInit = $.extend( true, {}, defaults, oInit );
oInit.aaData = tmpData;
// Map the initialisation options onto the settings object
_fnMap( oSettings.oFeatures, oInit, [