Last active
September 2, 2016 02:23
-
-
Save carlosrymer/74480e4b61c4cf22054b to your computer and use it in GitHub Desktop.
Using ngResource to create Resource Service in AngularJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
/** | |
* Resources service for interacting with the API | |
* @param {object} $resource | |
* @param {object} AppConfiguration | |
*/ | |
function Resources($resource, AppConfiguration) { | |
/** | |
* Category Resource | |
* @return {object} Resource object with methods for creating, getting, removing, and updating categories | |
*/ | |
function CategoryResource() { | |
return $resource(AppConfiguration.API + '/category/:id', {}, { | |
get: { | |
cache: true, | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/category/find' | |
}, | |
all: { | |
cache: true, | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/category' | |
} | |
}); | |
} | |
/** | |
* Document Resource | |
* @return {object} Resource object with methods for creating, getting, removing, and updating documents | |
*/ | |
function DocumentResource() { | |
return $resource(AppConfiguration.API + '/document/:id', {}, { | |
create: { | |
method: 'POST', | |
isArray: false, | |
url: AppConfiguration.API + '/document' | |
}, | |
get: { | |
cache: true, | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/document/find' | |
}, | |
remove: { | |
method: 'DELETE', | |
isArray: false | |
}, | |
update: { | |
method: 'PUT', | |
isArray: false | |
} | |
}); | |
} | |
/** | |
* User Resource | |
* @return {object} Resource object with methods for creating, getting, removing, and updating users | |
*/ | |
function UserResource() { | |
return $resource(AppConfiguration.API + '/user/:id', {}, { | |
autocomplete: { | |
cache: true, | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/user/autocomplete' | |
}, | |
create: { | |
method: 'POST', | |
isArray: false, | |
url: AppConfiguration.API + '/user' | |
}, | |
follow: { | |
method: 'GET', | |
isArray: false, | |
url: AppConfiguration.API + '/user/follow/:id' | |
}, | |
followers: { | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/user/followers/:id' | |
}, | |
following: { | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/user/following/:id' | |
}, | |
me: { | |
method: 'GET', | |
isArray: false, | |
url: AppConfiguration.API + '/user/me' | |
}, | |
get: { | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/user/find' | |
}, | |
remove: { | |
method: 'DELETE', | |
isArray: false | |
}, | |
search: { | |
method: 'GET', | |
isArray: true, | |
url: AppConfiguration.API + '/user/search' | |
}, | |
unfollow: { | |
method: 'GET', | |
isArray: false, | |
url: AppConfiguration.API + '/user/unfollow/:id' | |
}, | |
update: { | |
method: 'PUT', | |
isArray: false | |
} | |
}); | |
} | |
return { | |
category: new CategoryResource(), | |
document: new DocumentResource(), | |
user: new UserResource() | |
}; | |
} | |
// Inject dependencies | |
Resources.$inject = [ | |
'$resource', | |
'AppConfiguration' | |
]; | |
// Register module | |
angular.module('app.services.resources', [ | |
'ngResource', | |
'app.config' | |
]) | |
.service('Resources', Resources); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment