Skip to content

Commit

Permalink
Update: Allow a window to be passed in for CommonJS factory which g…
Browse files Browse the repository at this point in the history
…ives DataTables the ability to be used in a headless environment (server-side rendering for example)

- Factory builder redesigned to pass in window and document to the
  factory method, mandating a small update to the AMD and Browser
  loaders
- Main change in is the CommonJS loader which can now optionally have a
  window object passed in - if it is not passed in `window` will be used
  (if this is the case in a CommonJS environment without a root object
  being passed in an error will occur).
- DataTables caches a reference to the jQuery instance so the plug-ins
  can easily reference jQuery while retaining their current return of
  the module they define. This basically means that DataTables core will
  include jQuery for the plug-ins.
- This does increase the core library size by ~160 bytes which is rather
  frustrating, but I think this is the correct way to go about it
- With thanks to Evan Carroll for input on this:
  DataTables/Plugins#199
  • Loading branch information
AllanJard committed Nov 3, 2015
1 parent 27e48e8 commit 5ea5776
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
33 changes: 22 additions & 11 deletions js/DataTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,39 @@
/*jslint evil: true, undef: true, browser: true */
/*globals $,require,jQuery,define,_selector_run,_selector_opts,_selector_first,_selector_row_indexes,_ext,_Api,_api_register,_api_registerPlural,_re_new_lines,_re_html,_re_formatted_numeric,_re_escape_regex,_empty,_intVal,_numToDecimal,_isNumber,_isHtml,_htmlNumeric,_pluck,_pluck_order,_range,_stripHtml,_unique,_fnBuildAjax,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnAjaxDataSrc,_fnAddColumn,_fnColumnOptions,_fnAdjustColumnSizing,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnVisbleColumns,_fnGetColumns,_fnColumnTypes,_fnApplyColumnDefs,_fnHungarianMap,_fnCamelToHungarian,_fnLanguageCompat,_fnBrowserDetect,_fnAddData,_fnAddTr,_fnNodeToDataIndex,_fnNodeToColumnIndex,_fnGetCellData,_fnSetCellData,_fnSplitObjNotation,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnGetDataMaster,_fnClearTable,_fnDeleteIndex,_fnInvalidate,_fnGetRowElements,_fnCreateTr,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAddOptionsHtml,_fnDetectHeader,_fnGetUniqueThs,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnFilterCreateSearch,_fnEscapeRegex,_fnFilterData,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnInfoMacros,_fnInitialise,_fnInitComplete,_fnLengthChange,_fnFeatureHtmlLength,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnFeatureHtmlTable,_fnScrollDraw,_fnApplyToChildren,_fnCalculateColumnWidths,_fnThrottle,_fnConvertToWidth,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnSortFlatten,_fnSort,_fnSortAria,_fnSortListener,_fnSortAttachListener,_fnSortingClasses,_fnSortData,_fnSaveState,_fnLoadState,_fnSettingsFromNode,_fnLog,_fnMap,_fnBindAction,_fnCallbackReg,_fnCallbackFire,_fnLengthOverflow,_fnRenderer,_fnDataSource,_fnRowAttributes*/

(/** @lends <global> */function( window, document, undefined ) {

(function( factory ) {
"use strict";

if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery'], factory );
define( ['jquery'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function ($) {
// Get jQuery if it wasn't passed in
return factory( $ || require('jquery') );
module.exports = function (root, $) {
if ( ! root ) {
// CommonJS environments without a window global must pass a
// root. This will give an error otherwise
root = window;
}

if ( ! $ ) {
$ = typeof window !== 'undefined' ? // jQuery's factory checks for a global window
require('jquery') :
require('jquery')( root );
}

return factory( $, root, root.document );
};
}
else {
// Browser
factory( jQuery );
factory( jQuery, window, document );
}
}
(/** @lends <global> */function( $ ) {
(function( $, window, document, undefined ) {
"use strict";

/**
Expand Down Expand Up @@ -212,6 +223,9 @@
// jQuery access
$.fn.dataTable = DataTable;

// Provide access to the host jQuery object (circular reference)
DataTable.$ = $;

// Legacy aliases
$.fn.dataTableSettings = DataTable.settings;
$.fn.dataTableExt = DataTable.ext;
Expand Down Expand Up @@ -393,6 +407,3 @@

return $.fn.dataTable;
}));

}(window, document));

24 changes: 17 additions & 7 deletions js/integration/dataTables.bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,32 @@
(function( factory ){
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery', 'datatables.net'], factory );
define( ['jquery', 'datatables.net'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function ($) {
if ( ! $ ) { $ = require('jquery'); }
if ( ! $.fn.dataTable ) { require('datatables.net')($); }
module.exports = function (root, $) {
if ( ! root ) {
root = window;
}

if ( ! $ || ! $.fn.dataTable ) {
// Require DataTables, which attaches to jQuery, including
// jQuery if needed and have a $ property so we can access the
// jQuery object that is used
$ = require('datatables.net')(root, $).$;
}

factory( $ );
return factory( $, root, root.document );
};
}
else {
// Browser
factory( jQuery );
factory( jQuery, window, document );
}
}(function( $ ) {
}(function( $, window, document, undefined ) {
'use strict';
var DataTable = $.fn.dataTable;

Expand Down
21 changes: 14 additions & 7 deletions js/integration/dataTables.foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@
(function( factory ){
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery', 'datatables.net'], factory );
define( ['jquery', 'datatables.net'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function ($) {
if ( ! $ ) { $ = require('jquery'); }
if ( ! $.fn.dataTable ) { require('datatables.net')($); }
module.exports = function (root, $) {
if ( ! root ) {
root = window;
}

if ( ! $ || ! $.fn.dataTable ) {
$ = require('datatables.net')(root, $).$;
}

factory( $ );
return factory( $, root, root.document );
};
}
else {
// Browser
factory( jQuery );
factory( jQuery, window, document );
}
}(function( $ ) {
}(function( $, window, document, undefined ) {
'use strict';
var DataTable = $.fn.dataTable;

Expand Down
23 changes: 15 additions & 8 deletions js/integration/dataTables.jqueryui.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@
(function( factory ){
if ( typeof define === 'function' && define.amd ) {
// AMD
define( ['jquery', 'datatables.net'], factory );
define( ['jquery', 'datatables.net'], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function ($) {
if ( ! $ ) { $ = require('jquery'); }
if ( ! $.fn.dataTable ) { require('datatables.net')($); }
module.exports = function (root, $) {
if ( ! root ) {
root = window;
}

factory( $ );
if ( ! $ || ! $.fn.dataTable ) {
$ = require('datatables.net')(root, $).$;
}

return factory( $, root, root.document );
};
}
else if ( jQuery ) {
else {
// Browser
factory( jQuery );
factory( jQuery, window, document );
}
}(function( $ ) {
}(function( $, window, document, undefined ) {
'use strict';
var DataTable = $.fn.dataTable;

Expand Down

0 comments on commit 5ea5776

Please sign in to comment.