RestAngular

work with the best, forget about the REST, lol

where we're at in the stack

route

what we're calling for apis

url

what appears up in our browser  search bar

but before we dive in...

let's talk about where we've been

calling from the back

  • $http
  • $resource
  • Restangular

$http

  • official angular component
  • straightforward
  • harder to manage when scaling up
  • have to include q for promises
/* /apps/scripts/views/rolodex-http.js*/

function resolveJob($http, $q, $stateParams){

        var dfd = $q.defer();
        var jobs = $http
          .get('/jobs/' + $stateParams.id)
          .success(function(data){
            dfd.resolve(data);
          });

        return dfd.promise;
      }

$resource

  • official ng component
  • pretty clean
  • extend all the things
  • u can put code in a resource
  • chain $promise 
  • has some odd bugs
    (trailing slashes, colons)
/* /app/scripts/views/rolodex-resource.js */

  function resolveJobs(JobResource){
      return JobResource
        .list() //or query
        .$promise;
    }

/* /app/scripts/services/jobfactory.js */

angular.module('jobApp')
  .factory('JobResource', ['$resource', 
    function ($resource) {
        return $resource('/jobs/:id', null, {
            list: { 
                    method: 'GET', 
                    isArray: true
                  },
            somethingElse: { 
                            method: 'get', 
                            customStuff: 'yay'
                           }
        });
     }
]);

Restangular

  • api versioning
  • promises by default
  • one base url in one place
  • doesn't need to know every url you'll need upfront, will infer urls
  • nested RESTful resources
  • no $resource bugs
/* app/scripts/views/rolodex-restangular.js */

function resolveJobs(JobResourceRestangular){
    return JobResourceRestangular.getList();
  }


/* /apps/scripts/services/
    jobfactoryrestangular.js */

 angular.module('jobApp')
 .factory('JobResourceRestangular', 
  ['Restangular', function (Restangular) {
    return Restangular.all('jobs');
   }
 ]);

tl;dr

  • resource & restangular are sufficient upgrades from http for standard rest operations
  • upgrade to restangular if it solves ur edge cases
  • otherwise no pressure to upgrade from $resource

thanks <3

restangular

By amandaharlin

restangular

talking bout $http, $resource, and restangular. intro overview.

  • 715