1
0
mirror of https://github.com/DataTables/DataTables.git synced 2024-12-01 13:24:10 +01:00

New - API: plugin(), plugin.register() and plugin.deregister() methods

- When working with plug-ins such as TableTools, there is no clearly
  defined way at the moment to get at the plug-in instance, with each
  doing it its own way (TableTools as a static function, while Scroller
  attaches itself to the settings object and KeyTable is its own
  intialiser, amoung others...) so I'm introducing these methods to
  unify this.

- plugin() is used by DataTables users to get their plugin instance, for
  example `table.plugin('tabletools')` would get the TableTools
  instance allowing full access to its API.

- plugin.register() and plugin.deregister() are called by the plug-ins
  when they attach themselves to a DataTable. The 'extras' will all need
  to be updated to use this new method.
This commit is contained in:
Allan Jardine 2013-10-11 17:19:10 +01:00
parent 846c4d9c60
commit a9035942d0
2 changed files with 53 additions and 3 deletions

View File

@ -1 +1 @@
ddd0e5f5c9509cd8fa5630453cc05c9f68863644
730e2b88377acb44e9db177e107b9d42ac8bf63f

View File

@ -6038,7 +6038,7 @@
{
if ( ! this instanceof _Api ) {
throw 'DT API must be constructed as a new object';
// or should it do the 'new' for the caller
// or should it do the 'new' for the caller?
// return new _Api.apply( this, arguments );
}
@ -8241,6 +8241,49 @@
} );
_api.register( 'plugin()', function ( type ) {
var ctx = this.context;
if ( ! ctx.length ) {
return null;
}
var plugins = ctx[0].oPlugins[ type ];
return ! plugins ?
null :
plugins.length == 1 ?
plugins[0] :
plugins;
} );
_api.register( 'plugin.register()', function ( type, inst ) {
return this.iterator( 'table', function ( settings ) {
var plugins = settings.oPlugins;
if ( ! plugins[ type ] ) {
plugins[ type ] = [];
}
plugins[ type ].push( inst );
} );
} );
_api.register( 'plugin.deregister()', function ( type, inst ) {
return this.iterator( 'table', function ( settings ) {
var plugins = settings.oPlugins[ type ];
if ( plugins ) {
var idx = $.inArray( inst, plugins );
if ( idx >= 0 ) {
plugins.splice( idx, 1 );
}
}
} );
} );
_api.register( 'destroy()', function ( remove ) {
remove = remove || false;
@ -12616,7 +12659,14 @@
* @type array
* @default []
*/
"aLastSort": []
"aLastSort": [],
/**
* Stored plug-in instances
* @type object
* @default {}
*/
"oPlugins": {}
};
/**