Using __dt_wrapper to determine if an object is a DataTable object...fragile?

Using __dt_wrapper to determine if an object is a DataTable object...fragile?

Bob RodesBob Rodes Posts: 17Questions: 1Answers: 0
edited August 2016 in DataTables 1.10

I'm using this code to determine whether a given variable is referencing a DataTable object or not:

if(undefined != myObjectVariable.$ && myObjectVariable.$.__dt_wrapper) {
    alert('A DataTable Object');
} else {
    alert('Not a DataTable Object');
}

Since __dt_wrapper is an undocumented internal, I'm wondering how robust this is: might it not be around in future versions? I'm also concerned that there might be a set of variables where myObjectVariable.$ is defined, but myObjectVariable.$.__dt_wrapper is not or is false. (I don't really understand what the $ is about.) Is there a better way to do this?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    Answer ✓

    This looks like a duplicate of this github issue that was filed yesterday.

    The answer is to simply use the instanceof operator: myVar instanceof $.fn.dataTable.Api.

    Allan

  • Bob RodesBob Rodes Posts: 17Questions: 1Answers: 0
    edited August 2016

    Wow. We must have been on the same psychic wavelength! Thanks Allan. I tinkered with instanceof for a while, but never came up with the fn part of it. Will this work with both DataTable and dataTable?

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin
    edited August 2016 Answer ✓

    $.fn.dataTable.Api is the same as $.fn.DataTable.Api.

    If you mean $().dataTable(), that is a jQuery instance so you would use x instanceof $.

    Allan

This discussion has been closed.