fnDrawCallback function which accepts a callback itself

fnDrawCallback function which accepts a callback itself

joelonesjoelones Posts: 7Questions: 1Answers: 0
edited April 2014 in DataTables 1.8
I have the following:

[code]
var oTable = $('#patient_table').dataTable( {
....
"fnDrawCallback": loadModalLinks,
});
[/code]

loadModalLinks is defined in another js file which is included, loadModalLinks also takes in a callback function via the first argument, how would you write this? I'm also using jquery.

As the following does not work:

[code]
...
"fnDrawCallback": loadModalLinks(function () {})
[/code]

Replies

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    [code]
    fnDrawCallback: function () {
    loadModalLinks( function ... );
    }
    [/code]

    Before you were executing the function and assigning its return value to the draw callback.

    > I'm also using jquery.

    I hope so! DataTables is a jQuery plug-in after all :-)

    Allan
  • joelonesjoelones Posts: 7Questions: 1Answers: 0
    Thanks for the reply Allan, but that does not work.

    [code]
    "fnDrawCallback": function () {
    loadModalLinks(function() { console.log('test')} );
    }
    [/code]
    where:

    [code]
    function loadModalLinks(fnCallback) {
    ....
    if ( typeof fnCallback == 'function' && fnCallback !== null ) { fnCallback(); }
    ...
    }
    [/code]

    The error i get is:
    [quote]
    Uncaught TypeError: Object [object global] has no method 'getAttribute'
    [/quote]

    what am i missing?
  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    I don't see a call to `getAttribute` in your above code. Can you please link to a test case showing the issue so I can debug it.

    Allan
  • joelonesjoelones Posts: 7Questions: 1Answers: 0
    edited April 2014
    Thanks allan, I seemed to get it working by returning a function that works with the callback, complicated, not sure if there's an easier way.
    [code]
    function loadModalLinks(fnCallback) {
    return function() {
    ....
    $.post(url, function(data) {
    if (data.success){
    oTable.fnDraw(false);

    if ( typeof fnCallback == 'function' && fnCallback !== null ) {
    ....
    };
    }
    [/code]
    [code]
    "fnDrawCallback": loadModalLinks(function() {
    console.log('fnCallback code');
    }),
    [/code]

    The whole point was to execute loadModalLinks function and run the fnCallback if the post was successfully, in this case the console.log('fnCallback code');

    still not sure why this works now
This discussion has been closed.