I have a factory and a Controller.
Factory
vm.profiles = new ProfileFactory();
Controller
if (Object.keys(vm.profiles.items).length == 0) {
vm.noProfiles = true;
}
I have a prototype nextPage() in the ProfileFactory -> vm.profiles Object. This Method queries the backend. I want to set the vm.noProfiles Variable to true if there are no items in vm.profiles.items.
The Problem
It goes automatically inside the if statement, because the if doesn't wait until the item is populated through the prototype Method nextPage().
How can I force JavaScript to wait until it is populated? Is there a better solution?
//Edit
This ihere is part of the nextPage Method. Should I do the query in the controller and just return the promise from Restangular or $http?
// Get List of Posts
Restangular.allUrl('user', ApiConfig.baseUrl + ':' + ApiConfig.port + '/users').getList(query)
.then(function (items) {
//console.log(items);
if (items.length > 0) {
for (var i = 0; i < items.length; i++) {
this.items.push(items[i]);
}
if (this.offset < items.meta.page_count) {
this.offset += 1;
} else {
return;
}
}
this.busy = false;
}.bind(this));