DataTables as an AMD / UMD module

DataTables as an AMD / UMD module

timtuckertimtucker Posts: 48Questions: 0Answers: 0
edited February 2012 in General
I'm in the process of trying to convert to using a module loader and was curious -- have you given any thought to adding support for some of the AMD-based loaders to DataTables? (jQuery 1.7 apparently even supports it natively now)

Switching the way things are defined doesn't appear to be all that difficult -- from what I've been able to determine and try out so far, it's just:
[code]
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}
(/** @lends */function($) {

... DataTables code ...

}));
[/code]

instead of:
[code]
(/** @lends */function($, window, document, undefined) {

... DataTables code ...

}(jQuery, window, document, undefined));
[/code]

More details:
https://github.com/umdjs/umd
https://github.com/amdjs/amdjs-api/wiki/AMD

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Its certainly a possibility for future now that it is in jQuery code, but I don't want to have DataTables require anything other than jQuery for basic initialisation, and certainly of rate time being, I want to continue supporting jQuery 1.4+ (actually, its 1.3+ at the moment, but most sites have moved on now :-) ).

    So certainly something to consider in future (or for a fork if it is suitable / required for a particular project).

    Regards,
    Allan
  • timtuckertimtucker Posts: 48Questions: 0Answers: 0
    From everything that I've been reading so far, the upside of adding support for AMD-based module loaders is that it doesn't add any requirements -- everything should work exactly as-is with older versions of jQuery (or with newer versions of jQuery when not using a module loader).

    The only difference is that if a module loader that follows the AMD spec is detected, it uses the module loader to load the script, otherwise it just calls the fallback to execute the inner function directly with jQuery as a parameter (pretty much the same as the existing module pattern does now).

    The only part that I hadn't worked through was caching window/document/undefined as parameters to allow for faster local lookups and smaller code size when minified.
This discussion has been closed.