Api extention "namespace" support - Preventing extension authors from stepping on each other

Api extention "namespace" support - Preventing extension authors from stepping on each other

zeroonezeroone Posts: 1Questions: 0Answers: 0
edited April 2014 in Plug-ins
I'm pretty sure this isn't already supported by DT, so this is an enhancement request.

Of course javascript doesn't support namespaces, but this can be faked using objects. IE:
[code]var MyDtExtensions = (function() {
var me = {};

me.fnGetColumnVals = function( oSettings, iCol ) {
/* get all values in column iCol */
};

return me;
})();

var aColumn1Data = MyDtExtensions.fnGetColumnVals( oSettings, 1 );
[/code]

The problem with adding extension methods to DataTables via [code]$.fn.dataTableExt.oApi.fnMyExtensionMethod = function(oSettings) {...}[/code] is that extension/plugin authors could be stepping on each other with extension method names. My own idea and implementation of a 'fnGetColumnVals()' extension method may not necessarily be the same as some other extension author. They may already set a 'fnGetColumnVals()' extension method, and I've just overwritten it with my own which likely breaks their plugin entirely. A method name like 'fnGetColumnVals' is pretty generic and likely to be used by more than one extension/plugin author.

To fix, can you add support for setting a property of '$.fn.dataTableExt.oApi' that is an object, and by doing so propagate that object to the standard DT calling method 'oTable.MyDtExtensions'? This would allow plugin/extension authors and their users to guarantee an expected behavior and return value by calling something like [code]oTable.MyDtExtensions.fnGetColumnVals( 1 )[/code] vs relying on whoever polluted the general DT extension method space with an 'oTable.fnGetColumnVals' function definition of their own.

Or otherwise, supporting separation of extensions and plugins how you think is best.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    This is actually supported in the new API in v1.10 (not intentionally as part of the design of the API, but as an effect of its flexibility).

    I haven't written the new developer docs for 1.10 yet, but basically to add an API method to 1.10 you use the `$.fn.dataTable.Api.register` function:

    [code]
    jQuery.fn.dataTable.Api.register( 'sum()', function () {
    return this.flatten().reduce( function ( a, b ) {
    return (a*1) + (b*1); // cast values in-case they are strings
    } );
    } );
    [/code]

    The first parameter can include a namespace:

    [code]

    [code]
    jQuery.fn.dataTable.Api.register( 'allan.sum()', function () {
    ...
    } );
    [/code]

    which you could then access like `table.allan.sum()` .

    This comes about be causing of the chaining hierarchy system used y the DataTables API in 1.10 - for example you can have `rows().data()` or `rows().nodes()` .

    The new API is introduced here: http://next.datatables.net/manual/api

    Allan
This discussion has been closed.