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

Fix getList and getLast #242

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions src/angular-datatables.instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ angular.module('datatables.instances', [])
function dtInstances($q) {
var _instances = {};
// Promise for fetching the last DT instance
var _deferLastDTInstances = null;
var _deferLastDTInstance = $q.defer();
var _lastDTInstance = null;
// Promise for fetching the list of DT instances
var _deferDTInstances = null;
var _deferDTInstances = $q.defer();
var _dtInstances = null;
return {
register: register,
Expand All @@ -25,46 +25,38 @@ function dtInstances($q) {
dtInstance.dataTable = result.dataTable;

_instances[dtInstance.id] = dtInstance;
if (_deferLastDTInstances) {
_deferLastDTInstances.resolve(dtInstance);
}
if (_deferDTInstances) {
_deferDTInstances.resolve(_instances);
}
_lastDTInstance = dtInstance;

//previous promise
_deferDTInstances.resolve(_dtInstances);
_deferLastDTInstance.resolve(_lastDTInstance);

//new promise
_deferDTInstances = $q.defer();
_deferLastDTInstance = $q.defer();

_deferDTInstances.resolve(_dtInstances);
_deferLastDTInstance.resolve(_lastDTInstance);

return dtInstance;
}

function getLast() {
var defer = $q.defer();
if (!_lastDTInstance) {
_deferLastDTInstances = $q.defer();
_lastDTInstance = _deferLastDTInstances.promise;
}
_lastDTInstance.then(function(dtInstance) {
defer.resolve(dtInstance);
// Reset the promise
_deferLastDTInstances = null;
_lastDTInstance = null;
_deferLastDTInstance.promise.then(function (lastInstance) {
defer.resolve(lastInstance);
});
return defer.promise;
}

function getList() {
var defer = $q.defer();
if (!_dtInstances) {
_deferDTInstances = $q.defer();
_dtInstances = _deferDTInstances.promise;
}
_dtInstances.then(function(instances) {
_deferDTInstances.promise.then(function (instances) {
defer.resolve(instances);
// Reset the promise
_deferDTInstances = null;
_dtInstances = null;
});
return defer.promise;
}
}

function dtInstanceFactory() {
var DTInstance = {
reloadData: reloadData,
Expand Down