Having an issue with handling multiple ajax requests. In my scenario, there is a form with a button that triggers a service upon clicking it. This service is responsible for loading a list of items into a table, but currently it only loads one item at a time when the button is clicked.
The problem arises when the button is clicked multiple times - the same item gets duplicated in the table each time it's loaded.
I am looking for a solution to prevent this duplication while waiting for the response from the first request.
Current NG Service
var getItems = function () {
var def = $q.defer();
Items.get().then(function (items) {
def.resolve(items);
}, function (err) {
...
});
};
I tried modifying the code like this:
var def = false;
var getItems = function () {
def = $q.defer();
Items.get().then(function (items) {
def.resolve(items);
}, function (err) {
...
});
};
Setting def = false
seemed to prevent duplication, but I'm unsure if this is the correct approach. Is resetting the previous request to false
a valid solution?