I have the following, very simplified snippet:
c.then(function(data){
vm.checkedOut = data;
});
It will either return a record, or it will not. Either way, the promise does return an object--it just might not have any record data attached.
So far, so good. Here's my question: how can I detect recordless responses in my views?
I was doing this:
<div ng-if="vm.checkedOut.loaneeName !== undefined">
checked out!
</div>
loaneeName
is a property of the record and it will be undefined if no record is returned. Kind of sloppy, but it works.
However this was how I displayed the opposite message:
<div ng-if="vm.checkedOut.loaneeName === undefined">
not checked out!
</div>
Because vm.checkedOut.loaneeName
will always be undefined when the page first loads (and waiting for responses from the API), the "not checked out!" message appears, at least for a while, which could be very bad if the connection speed is slow enough.
To fix this issue, I've done the following:
c.then(function(data){
vm.checkedOut = data;
if(vm.checkedOut.loaneeName === undefined){
vm.forRealsCheckedOut = true;
}
else {
vm.forRealsCheckedOut = false;
}
});
And then:
<div ng-if="vm.forRealsCheckedOut === false">
not checked out!
</div>
...
This works, but it also strikes me as a slightly gross workaround for what I assume should be a common problem.
I am not the first person to ask a question like this, but I don't have an array and other answers aren't much help.
Is there a better way?