Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 8e2fa92

Browse files
committed
Data reload issues multiple requests #85
1 parent ed35e5d commit 8e2fa92

4 files changed

+66
-67
lines changed

dist/angular-datatables.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,16 @@
374374
return function postLink($scope, $elem, iAttrs, ctrl) {
375375
$scope.$watch('[dtOptions, dtColumns, dtColumnDefs]', function (newVal, oldVal) {
376376
if (newVal !== oldVal) {
377-
ctrl.render($elem, ctrl.buildOptions(), _staticHTML);
377+
var newDTOptions = newVal[0], oldDTOptions = oldVal[0];
378+
// Do not rerender if we want to reload. There are already
379+
// some watchers in the renderers.
380+
if (!newDTOptions.reload || newDTOptions.sAjaxSource !== oldDTOptions.sAjaxSource) {
381+
ctrl.render($elem, ctrl.buildOptions(), _staticHTML);
382+
} else {
383+
// The reload attribute is set to false here in order
384+
// to recall this watcher again
385+
newDTOptions.reload = false;
386+
}
378387
}
379388
}, true);
380389
ctrl.showLoading($elem);
@@ -970,7 +979,10 @@
970979
*/
971980
return {
972981
create: function (options) {
973-
var oTable, _render = function (options, $elem, data, $scope) {
982+
var oTable,
983+
// Reloading data call the "render()" function again, so it
984+
// might $watch again. So this flag is here to prevent that!
985+
_watcherInitialized = false, _render = function (options, $elem, data, $scope) {
974986
options.aaData = data;
975987
// Add $timeout to be sure that angular has finished rendering before calling datatables
976988
$timeout(function () {
@@ -1001,27 +1013,27 @@
10011013
}
10021014
_loadedPromise.then(_whenLoaded);
10031015
}, _reload = function (fnPromise) {
1004-
if (_loadedPromise) {
1005-
_loadedPromise.then(function () {
1016+
if (angular.isDefined(fnPromise)) {
1017+
if (_loadedPromise) {
1018+
_loadedPromise.then(function () {
1019+
_startLoading(fnPromise);
1020+
});
1021+
} else {
10061022
_startLoading(fnPromise);
1007-
});
1023+
}
10081024
} else {
1009-
_startLoading(fnPromise);
1025+
throw new Error('You must provide a promise or a function that returns a promise!');
10101026
}
10111027
};
1012-
$scope.$watch('dtOptions.fnPromise', function (fnPromise) {
1013-
if (angular.isDefined(fnPromise)) {
1014-
_reload(fnPromise);
1015-
} else {
1016-
throw new Error('You must provide a promise or a function that returns a promise!');
1017-
}
1018-
});
1019-
$scope.$watch('dtOptions.reload', function (reload) {
1020-
if (reload) {
1021-
$scope.dtOptions.reload = false;
1022-
_reload($scope.dtOptions.fnPromise);
1023-
}
1024-
});
1028+
if (!_watcherInitialized) {
1029+
$scope.$watch('dtOptions.fnPromise', function (fnPromise, oldPromise) {
1030+
if (fnPromise !== oldPromise) {
1031+
_reload(fnPromise);
1032+
}
1033+
});
1034+
_watcherInitialized = true;
1035+
}
1036+
_reload($scope.dtOptions.fnPromise);
10251037
return _this;
10261038
};
10271039
return renderer;
@@ -1081,20 +1093,7 @@
10811093
if (angular.isUndefined(_this.options.aoColumns)) {
10821094
_this.options.aoColumns = DT_DEFAULT_OPTIONS.aoColumns;
10831095
}
1084-
$scope.$watch('dtOptions.sAjaxSource', function (sAjaxSource, oldAjaxSource) {
1085-
if (sAjaxSource !== oldAjaxSource) {
1086-
_setOptionsAndRender(_this.options, sAjaxSource, $elem, $scope);
1087-
}
1088-
}, true);
1089-
$scope.$watch('dtOptions.reload', function (reload, oldReload) {
1090-
if (reload !== oldReload) {
1091-
if (reload) {
1092-
$scope.dtOptions.reload = false;
1093-
_render(options, $elem, $scope);
1094-
}
1095-
}
1096-
}, true);
1097-
_setOptionsAndRender(_this.options, _this.options.sAjaxDSource, $elem, $scope);
1096+
_setOptionsAndRender(_this.options, _this.options.sAjaxSource, $elem, $scope);
10981097
return this;
10991098
};
11001099
return renderer;

0 commit comments

Comments
 (0)