Let's say my API returns a list of dates a product was viewed for simplicity. Some of the products were never viewed, and an empty list will be returned. When this happens, I want to show:
This product hasn't been viewed yet!
and hide the table with product view dates. I do this in the controller returning IHttpActionResult:
if (itemViewedDto.Count > 0)
{
return Ok(itemViewedDto);
}
else
{
return Ok(new {message = "No data"});
}
On the client I do (itemViewed is just the http response payload):
$scope.dataExists = function(){
if($scope.itemViewed.hasOwnProperty(message)){
return true;
} else {
return false;
}
}
And then show response accordingly using ng-if or whatever to see if there was a response or not?
Is there anything wrong with this? Is this an accepted pattern? If not, what's a better way to do it? I am asking conceptually, not for this particular situation.
Currently its work for me, just I'm curious to know if it's wrong.
dfsdfsdfsdfsdfsdfsdfsgfgqfgqdgsdfqfqsfqsfqsfqsfqsf