-1

I know that is it possible to create custom methods using the AngularJs $resource, but I would like to know if it is possible to tell the person that is using the resource what parameter they should provide when calling it.

I am not talking about default values, but more like a guideline of what to provide to call the $resource.

$resource("/api/myEntity/:id", { id: '@id' }, {
    getBySlug: {
        method: "GET",
        url: "/api/MyEntity/GetBySlug/"
        //something like : paramsToProvide : {slug : "", etc.}
    },
});

//...

myEntity.myCustomMethod({}, function(){
    //callback...
});
RPDeshaies
  • 1,826
  • 1
  • 24
  • 29

1 Answers1

0

Create a factory which exposes an API, and throw an error or log a warning if a function is called without required parameters.

var myEntity = $resource(...);

return {
   getBySlug: function getBySlug(slug) {
      if (slug === undefined) {
         throw new Error('Please provide a slug');
      }
      return myEntity.getBySlug({slug: slug}).$promise;
   }
};
Thomas Roch
  • 1,208
  • 8
  • 19
  • This would remove all the $save, $get, $query and $delete methods from a default $resource, so I don't think that would be a viable option. – RPDeshaies May 07 '15 at 13:50