Callback hooks, instead of a single callback function

Callback hooks, instead of a single callback function

fbasfbas Posts: 1,094Questions: 4Answers: 0
edited August 2011 in General
I think it is increasingly happening that people are feeling constrained with some of the callbacks - if they already have code or a plug-in that uses fnDrawCallback, for instance, they feel they need to find another callback to put a new piece of code.

However, with event triggers, you can use JQuery to subscribe ("bind", "live") to events and have multiple handlers respond. JQuery allows you to pass additional data to the handler, so for callbacks that have parameters, the parameters can be added to the 'data'.

The code below uses the console to log several events. with a datestamp.

This approach isn't ideal for every situation, like times when you are using the callback to modify the value of a cell or row, or callbacks that require you to return a value, like "fnInfoCallback": function( oSettings, iStart, iEnd, iMax, iTotal, sPre ) { return string; } or "fnPreDrawCallback": function( oSettings ) { return true || false; }.

[code]
$('#example').live('fnInitComplete', function(e, data) { console.log('fnInitComplete', data, new Date()); });
$('#example').live('fnRowCallback', function(e, data) { console.log('fnRowCallback', data, new Date()); });
$('#example').live('fnCookieCallback', function(e, data) { console.log('fnCookieCallback', data, new Date()); });
$('#example').live('fnHeaderCallback', function(e, data) { console.log('fnHeaderCallback', data, new Date()); });
$('#example').live('fnFooterCallback', function(e, data) { console.log('fnFooterCallback', data, new Date()); });
$('#example').live('fnStateLoadCallback', function(e, data) { console.log('fnStateLoadCallback', data, new Date()); });
$('#example').live('fnStateSaveCallback', function(e, data) { console.log('fnStateSaveCallback', data, new Date()); });

$(document).ready(function() {
$('#example').dataTable({
fnInitComplete: function() { $(this).trigger('fnInitComplete'); },
fnCookieCallback: function(sName, oData, sExpires, sPath) { $(this).trigger('fnCookieCallback', { sName:sName, oData:oData, sExpires:sExpires, sPath:sPath }); },
fnDrawCallback: function() { $(this).trigger('fnDrawCallback'); },
fnFooterCallback: function( nFooterNode, aasData, iStart, iEnd, aiDisplay ) { $(this).trigger('fnFooterCallback', { nFooterNode:nFooterNode, aasData:aasData, iStart:iStart, iEnd:iEnd, aiDisplay:aiDisplay }); },
fnHeaderCallback: function( nHeaderNode, aasData, iStart, iEnd, aiDisplay ) { $(this).trigger('fnHeaderCallback', { nHeaderNode:nHeaderNode, aasData:aasData, iStart:iStart, iEnd:iEnd, aiDisplay:aiDisplay }); },
fnRowCallback: function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) { $(this).trigger('fnRowCallback', { nRow:nRow, aData:aData, iDisplayIndex:iDisplayIndex, iDisplayIndexFull:iDisplayIndexFull }); return nRow; },
fnStateLoadCallback: function ( oSettings, oData ) { $(this).trigger('fnStateLoadCallback', { oSettings:oSettings, oData:oData }); },
fnStateSaveCallback: function ( oSettings, sValue ) { $(this).trigger('fnStateSaveCallback', { oSettings:oSettings, sValue:sValue }); }

})
});

[/code]

results:
[code]
fnRowCallback Object Thu Aug 04 2011 09:01:07 GMT-0500 (Central Daylight Time) trigger.htm:17

fnRowCallback Object Thu Aug 04 2011 09:01:07 GMT-0500 (Central Daylight Time) trigger.htm:17

fnRowCallback Object Thu Aug 04 2011 09:01:07 GMT-0500 (Central Daylight Time) trigger.htm:17

fnRowCallback Object Thu Aug 04 2011 09:01:07 GMT-0500 (Central Daylight Time) trigger.htm:17

fnHeaderCallback Object Thu Aug 04 2011 09:01:07 GMT-0500 (Central Daylight Time) trigger.htm:19

fnFooterCallback Object Thu Aug 04 2011 09:01:07 GMT-0500 (Central Daylight Time) trigger.htm:20

fnInitComplete undefined Thu Aug 04 2011 09:01:07 GMT-0500 (Central Daylight Time) trigger.htm:16
[/code]
This discussion has been closed.