Skip to content

Instantly share code, notes, and snippets.

@thomasnordlund
Created November 15, 2012 21:53
Show Gist options
  • Save thomasnordlund/4081572 to your computer and use it in GitHub Desktop.
Save thomasnordlund/4081572 to your computer and use it in GitHub Desktop.
angular: $resource
/*
* Dependencies
*
* You must reference the angular-resource js script file.
* Then you must declare a module dependency on 'ngResource' via angular.module('myapp', ['ngResource']).
*/
// Parameters
$resource(url[, paramDefaults][, actions]);
// Example
var user = $resource(
/*
* A parameterized URL template with parameters prefixed by : as in /user/:username.
*/
'/user/:username',
/*
* paramDefaults(optional) – {Object=} – Default values for url parameters.
*
* Each key value in parameter object is bound to url template if present; excess keys are appended to the url search query after the ?.
* If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object.
*
* Given a template /user/:username and parameter {user:'@user', username:'@username', parameter:'parameter'} results in URL /user/username?parameter=parameter.
*/
{
user:'@user',
username:'@username',
parameter:'parameter'
},
/*
* Hash with declaration of custom action that should extend the default set of resource actions.
*
* action – {string} – The name of action. This name becomes the name of the method on your resource object.
* method – {string} – HTTP request method. Valid methods are: GET, POST, PUT, DELETE, and JSONP
* params – {object=} – Optional set of pre-bound parameters for this action.
* isArray – {boolean=} – If true then the returned object for this action is an array, see returns section.
*
*/
{
action: {method:?, params:?, isArray:?},
action: {method:?, params:?, isArray:?},
action: {method:?, params:?, isArray:?}
}
);
/*
* Returns
*
* A resource "class" object with methods for the default set of resource actions,
* optionally extended with custom actions.
*
* {
'get': {method:'GET'},
'save': {method:'POST'},
'query': {method:'GET', isArray:true},
'remove': {method:'DELETE'},
'delete': {method:'DELETE'}
* };
*
* The action methods on the class object or instance object can be invoked with the following parameters:
*
* HTTP GET "class" actions: Resource.action([parameters], [success], [error])
* non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
* non-GET instance actions: instance.$action([parameters], [success], [error])
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment