Testing whether an object is an angularJS $ resource

A simple (seeming) question - I'm trying to do a simple sanity check in my AngularJS controller to make sure my $ resource is actually created as such. This is a fairly large application, but for example:

.factory('AccountSearchService_XHR', ["$resource", function($resource) { var baseUrl = "http://localhost\\:8081/api/:version/accounts/:accountNumber"; return $resource(baseUrl, { version: "@version", accountNumber: "@accountNumber" }, { get: {method: 'GET', isArray: false} }); }]); 

Then later in the controller:

 $scope.accountObj.currentAccount = AccountSearchService_XHR.get({ version: "v1", accountNumber: "1234" }, function(result) {... etc etc}); 

The call to my API works fine, everything returns data as I expect, but I would like to check if there is a $scope.accountObj.currentAccount resource before trying to make a .get call (note the super important capital) R ').

When I check the $scope.accountObj.currentAccount object in the chrome debugger, it looks like this:

 Resource {accountHolderName: Object, socialSecurityNumer: null, birthDate: "05/14/1965", maritalStatus: ...} 

Due to some complexity of my setup, although sometimes it is overwritten as a regular object ( typeof returns "object" ), but checking it in the debugger confirms that it has lost its Resource status.

So, does anyone know how to check if it is a $ resource? Almost like typeof $scope.accountObj.currentAccount returns "Resource" ? Or maybe the best way is best to ensure that everything connects everything that is appropriate and respectable?

All the SO articles I've seen while searching revolve around actual Jasmine testing.

Thanks in advance.

+4
source share
3 answers

Check it out through instanceof yourVariable === "Resource" . Since a resource is an object, the type will always be returned as an object, but if you check it with an instance of the resource class, which should work fine.

-3
source

@tengen you need to enter the type you want to check instead of $ resource.

All resources are instances of the "class" "Resource", but they are a function that is defined inside the factory method of the $ resource service, so you do not have external visibility to use it with the instanceof operator.

However, you are completing the creation of $ resource with your own custom type AccountSearchService_XHR and what you need to do to check.

You need to enter AccountSearchService_XHR in your code, and then execute myRef instanceof AccountSearchService_XHR, and that will be === true.

0
source

Digging out an old question that my intern just did. A simple solution:

 if ($scope.accountObj.currentAccount instanceof AccountSearchService_XHR) return 'This is a AccountSearchService_XHR Resource'; else return 'This is not a AccountSearchService_XHR Resource'; 

which with proper names ( Users is $resource ) and a real-case scenario should lead you to write something like this:

 if (!(this.user instanceof Users)) this.user = new Users(this.user); this.user.$update(); 
0
source

Source: https://habr.com/ru/post/1501670/


All Articles