|
214 | 214 | 'DT_DEFAULT_OPTIONS',
|
215 | 215 | '$timeout',
|
216 | 216 | function ($http, DT_DEFAULT_OPTIONS, $timeout) {
|
| 217 | + var _renderDataTable = function ($elem, options) { |
| 218 | + // Add $timeout to be sure that angular has finished rendering before calling datatables |
| 219 | + $timeout(function () { |
| 220 | + $elem.dataTable(options); |
| 221 | + }, 0, false); |
| 222 | + }; |
217 | 223 | return {
|
218 | 224 | restrict: 'A',
|
219 | 225 | scope: {
|
|
228 | 234 | if (angular.isArray($scope.dtColumns)) {
|
229 | 235 | options.aoColumns = $scope.dtColumns;
|
230 | 236 | }
|
231 |
| - // Define defaults values in case it is an ajax datatables |
232 |
| - if (angular.isDefined(options.sAjaxSource)) { |
233 |
| - if (angular.isUndefined(options.sAjaxDataProp)) { |
234 |
| - options.sAjaxDataProp = DT_DEFAULT_OPTIONS.sAjaxDataProp; |
235 |
| - } |
236 |
| - if (angular.isUndefined(options.aoColumns)) { |
237 |
| - options.aoColumns = DT_DEFAULT_OPTIONS.aoColumns; |
| 237 | + if (angular.isDefined(options.dataPromise)) { |
| 238 | + options.dataPromise.then(function (data) { |
| 239 | + options.aaData = data; |
| 240 | + _renderDataTable($elem, options); |
| 241 | + }); |
| 242 | + } else { |
| 243 | + // Define defaults values in case it is an ajax datatables |
| 244 | + if (angular.isDefined(options.sAjaxSource)) { |
| 245 | + if (angular.isUndefined(options.sAjaxDataProp)) { |
| 246 | + options.sAjaxDataProp = DT_DEFAULT_OPTIONS.sAjaxDataProp; |
| 247 | + } |
| 248 | + if (angular.isUndefined(options.aoColumns)) { |
| 249 | + options.aoColumns = DT_DEFAULT_OPTIONS.aoColumns; |
| 250 | + } |
238 | 251 | }
|
| 252 | + _renderDataTable($elem, options); |
239 | 253 | }
|
240 |
| - // Add $timeout to be sure that angular has finished rendering before calling datatables |
241 |
| - $timeout(function () { |
242 |
| - $elem.dataTable(options); |
243 |
| - }, 0, false); |
244 | 254 | } else {
|
245 |
| - // Add $timeout to be sure that angular has finished rendering before calling datatables |
246 |
| - $timeout(function () { |
247 |
| - $elem.dataTable(); |
248 |
| - }, 0, false); |
| 255 | + _renderDataTable($elem); |
249 | 256 | }
|
250 | 257 | }
|
251 | 258 | };
|
|
289 | 296 | /**
|
290 | 297 | * The wrapped datatables options class
|
291 | 298 | * @param sAjaxSource the ajax source to fetch the data
|
| 299 | + * @param dataPromise the promise to fetch the data |
292 | 300 | */
|
293 |
| - var DTOptions = function (sAjaxSource) { |
| 301 | + var DTOptions = function (sAjaxSource, dataPromise) { |
294 | 302 | this.sAjaxSource = sAjaxSource;
|
| 303 | + this.dataPromise = dataPromise; |
295 | 304 | /**
|
296 | 305 | * Optional class to handle undefined or null
|
297 | 306 | * @param obj the object to wrap
|
|
416 | 425 | this.iDisplayLength = iDisplayLength;
|
417 | 426 | return this;
|
418 | 427 | };
|
| 428 | + /** |
| 429 | + * Set the promise to fetch the data |
| 430 | + * @param dataPromise the promise |
| 431 | + * @returns {DTOptions} the options |
| 432 | + */ |
| 433 | + this.withDataPromise = function (dataPromise) { |
| 434 | + this.dataPromise = dataPromise; |
| 435 | + return this; |
| 436 | + }; |
419 | 437 | // BOOTSTRAP INTEGRATION ---------
|
420 | 438 | // See http://getbootstrap.com
|
421 | 439 | /**
|
|
556 | 574 | return new DTOptions();
|
557 | 575 | },
|
558 | 576 | fromSource: function (sAjaxSource) {
|
559 |
| - return new DTOptions(sAjaxSource); |
| 577 | + return new DTOptions(sAjaxSource, null); |
| 578 | + }, |
| 579 | + fromPromise: function (dataPromise) { |
| 580 | + return new DTOptions(null, dataPromise); |
560 | 581 | }
|
561 | 582 | };
|
562 | 583 | }
|
|
0 commit comments