Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks to Andy Joslin. I picked his idea of wrapping the resource actions. The service for the resource looks like this now:</p> <pre><code>.factory('Todo', ['$resource', 'TokenHandler', function($resource, tokenHandler) { var resource = $resource('http://localhost:port/todos/:id', { port:":3001", id:'@id' }, { update: {method: 'PUT'} }); resource = tokenHandler.wrapActions( resource, ["query", "update"] ); return resource; }]) </code></pre> <p>As you can see the resource is defined the usual way in the first place. In my example this includes a custom action called <code>update</code>. Afterwards the resource is overwritten by the return of the <code>tokenHandler.wrapAction()</code> method which takes the resource and an array of actions as parameters.</p> <p>As you would expect the latter method actually wraps the actions to include the auth token in every request and returns a modified resource. So let's have a look at the code for that:</p> <pre><code>.factory('TokenHandler', function() { var tokenHandler = {}; var token = "none"; tokenHandler.set = function( newToken ) { token = newToken; }; tokenHandler.get = function() { return token; }; // wrap given actions of a resource to send auth token with every // request tokenHandler.wrapActions = function( resource, actions ) { // copy original resource var wrappedResource = resource; for (var i=0; i &lt; actions.length; i++) { tokenWrapper( wrappedResource, actions[i] ); }; // return modified copy of resource return wrappedResource; }; // wraps resource action to send request with auth token var tokenWrapper = function( resource, action ) { // copy original action resource['_' + action] = resource[action]; // create new action wrapping the original and sending token resource[action] = function( data, success, error){ return resource['_' + action]( angular.extend({}, data || {}, {access_token: tokenHandler.get()}), success, error ); }; }; return tokenHandler; }); </code></pre> <p>As you can see the <code>wrapActions()</code> method creates a copy of the resource from it's parameters and loops through the <code>actions</code> array to call another function <code>tokenWrapper()</code> for every action. In the end it returns the modified copy of the resource.</p> <p>The <code>tokenWrapper</code>method first of all creates a copy of preexisting resource action. This copy has a trailing underscore. So <code>query()</code>becomes <code>_query()</code>. Afterwards a new method overwrites the original <code>query()</code> method. This new method wraps <code>_query()</code>, as suggested by Andy Joslin, to provide the auth token with every request send through that action.</p> <p>The good thing with this approach is, that we still can use the predefined actions which come with every angularjs resource (get, query, save, etc.), without having to redefine them. And in the rest of the code (within controllers for example) we can use the default action name.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload