Hello I have three tables with linked records (record in next table has link on record in previous table). Now I would like to remove record from a table with his children in next tables. I wanted to use recursion, but I have problem with promise. Here is my code:
$scope.removeItem = function (table, itemId) {
var nextTableIndex = table.index + 1;
dbService.remove(table, [{
by: table.parentColumn == undefined ? "ID" : table.parentColumn, values: itemId
}]);
while (nextTableIndex < config.tables.length) {
var nextTable = config.tables[nextTableIndex];
dbService.getTableItems(nextTable.id, ["ID"], [{ by: nextTable.parentColumn, values: itemId }])
.then(function (data) {
var ids = select(data, "ID"); //return array with all ids ([1,2,3])
$scope.removeItem(config.tables[table.index + 1], ids);
});
nextTableIndex++;
}
}
dbService.getTableItems = function(tableId, columns, where){ //return promise
return $http.get(createUrl(tableId, columns, where));
}
Removing only from first table and last table. In recursion use last nextTable because first it performed whole while and than it performed "then" function. I need first load data from database and than call next code.
It is a possibility how to load data immediately? Something like: dbService.getTableItems(...).toArray()
Thank you