api - AngularJS Resource Calling Several times -


im building restful app , using angular view. want use resources since best approach it, follow how-to's , made tweaks own include header api token, code end this:

fcbmixapp.factory('resources', ['$resource',     function ($resource) {         return {             seminary: function (apitoken) {                 return $resource('api/seminaries/:seminary', {}, {                     save: {                         method: 'post',                         headers: {                             'authorization': 'bearer ' + apitoken                         }                     },                     update: {                         method: 'put',                         headers: {                             'authorization': 'bearer ' + apitoken                         }                     }                 });             },             attendant: function (apitoken) {                 return $resource('api/attendants/:attendant', {}, {                     save: {                         method: 'post',                         headers: {                             'authorization': 'bearer ' + apitoken                         }                     },                     update: {                         method: 'put',                         headers: {                             'authorization': 'bearer ' + apitoken                         }                     }                 });             }         }     }]); 

but when call on controller this:

var seminary = resources.seminary(user.getauthdata().access_token); 

i dont expect line make request api, does. code follows:

seminary.query(function (data) {                 $scope.seminaries = data;             }); 

so make 2 calls.

what im doing wrong, or should change.

thanks in advance.

you should set header token:

    $http.defaults.headers.common["authorization"] = 'bearer' + apitoken; 

and not in resource itself. should set when user logged in first time, send on requests.

also consider resource looking this, , making separate 1 attendant:

   fcbmixapp.factory('resources', ['$resource', function ($resource) {         function setrequestdata(data) {             var requestdata = new object();             requestdata.seminary = data;             return angular.tojson(requestdata);         }         return $resource('api/seminaries/:seminary', {}, {             save: {                 method: 'post',                 headers: {"content-type": "application/json"},                 transformrequest: setrequestdata             },             update: {                 method: 'put',                 headers: {"content-type": "application/json"},                 transformrequest: setrequestdata             }         });     }]); 

Comments