DataTables as an AMD / UMD module
DataTables as an AMD / UMD module
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
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
This discussion has been closed.
Replies
So certainly something to consider in future (or for a fork if it is suitable / required for a particular project).
Regards,
Allan
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.