fnUpdate in 1.9.4: consistency issue when adding objects
fnUpdate in 1.9.4: consistency issue when adding objects
psmerd
Posts: 9Questions: 0Answers: 0
I see a consistency problem with fnUpdate regarding using an object to update the entire row. The code is specifically checking for a 'plain' object, created anonymously, or with new Object. The problem is this same requirement is not enforced when the table is first created.
I have created a custom class (we'll call it FooClass). I create an array of foos and set aaData to this array on init.
foos = [];
...
foo = new FooClass();
...
foos.push(foo);
$dataTable = $('#container').dataTable({aaData: foos, ...});
This works nicely.
Later, I edit the foo and want to replace the foo object in the row using fnUpdate.
rowElem = (a tr node in the #container table);
foo = new FooClass({name: 'someName'});
$dataTable.fnUpdate(foo, rowElem);
This fails, because foo does not pass the $.isPlainObject() test on line 6159:
6159: else if ( $.isPlainObject(mData) && iColumn === undefined )
The underlying issue is that this check for isPlainObject is not performed initially.
My choice would be to allow any object to be used in fnUpdate, not not restrict them to anonymous objects with no prototype.
John Gillespie
I have created a custom class (we'll call it FooClass). I create an array of foos and set aaData to this array on init.
foos = [];
...
foo = new FooClass();
...
foos.push(foo);
$dataTable = $('#container').dataTable({aaData: foos, ...});
This works nicely.
Later, I edit the foo and want to replace the foo object in the row using fnUpdate.
rowElem = (a tr node in the #container table);
foo = new FooClass({name: 'someName'});
$dataTable.fnUpdate(foo, rowElem);
This fails, because foo does not pass the $.isPlainObject() test on line 6159:
6159: else if ( $.isPlainObject(mData) && iColumn === undefined )
The underlying issue is that this check for isPlainObject is not performed initially.
My choice would be to allow any object to be used in fnUpdate, not not restrict them to anonymous objects with no prototype.
John Gillespie
This discussion has been closed.
Replies
Thanks very much for posting that, and detailing the issue you are facing so succinctly.
You are absolutely correct that DataTables should really pick one method only and stick with it. I'm also in full agreement with your suggestion that any object should be used, not just plain objects.
That approach wasn't possible in DataTables before v1.10 (which is still in development, although the required change has been committed), as DataTables would always copy objects when adding new rows (this in turn was legacy from v1.0 with fnRender which could change the data - v1.10 is finally cutting those strings).
As such, now it is possible to use 'instances' as the data source, or otherwise non-plain objects. In combination with mData / mRender as functions, this means that we can use 'class instances' (or as close as we get in Javascript) as data source objects for DataTables.
So ultimately the check for plain objects in v1.10 is not only redundant, it is in fact wrong. I've not made the required change for that yet, but I will do shortly thanks to your suggestion.
Thanks for flagging this up!
Regards,
Allan
Allan