Created
March 16, 2016 03:18
-
-
Save blacktambourine/29fec26e4abffcf2d6f2 to your computer and use it in GitHub Desktop.
Angular JS Services
This file contains hidden or 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
var corpServices = angular.module('corpApp.corpServices', ['ngResource']); | |
(function () | |
{ | |
//#region Common Service Methods | |
var serviceRootPath = '/api/'; | |
//add anti-forgery token to all Authenticated GET and POST requests | |
var AntiForgeryRequest = function (enableCache) | |
{ | |
var details = | |
{ | |
'get': { method: 'GET', headers: { 'X-XSRF-Token': angular.element('input[name="__RequestVerificationToken"]').attr('value') }, cache: enableCache }, | |
'post': { method: 'POST', headers: { 'X-XSRF-Token': angular.element('input[name="__RequestVerificationToken"]').attr('value') } } | |
}; | |
return details; | |
} | |
//regular request for unauthenticated users | |
var AnonymousRequest = function (enableCache) | |
{ | |
var details = | |
{ | |
'get': { method: 'GET', cache: enableCache }, | |
'post': { method: 'POST' } | |
}; | |
return details; | |
} | |
var newService = function (serviceRoute, parameterObj, enableCache, authenticatedOnly) | |
{ | |
var service = | |
[ | |
'$resource', function ($resource) | |
{ | |
if (authenticatedOnly) | |
{ | |
return $resource(serviceRoute, parameterObj, AntiForgeryRequest(enableCache)); | |
} | |
else | |
{ | |
return $resource(serviceRoute, parameterObj, AnonymousRequest(enableCache)); | |
} | |
} | |
]; | |
return service; | |
} | |
//#endregion | |
//#region Service Definitions | |
//newService = function (serviceRoute, parameterObj, enableCache, authenticatedOnly) | |
//#region Events | |
corpServices.factory('EventListingService', newService(serviceRootPath + 'EventListing/:id', { id: '@id' }, true, false)); | |
corpServices.factory('RsvpService', newService(serviceRootPath + 'SendRsvp/:id', { id: '@id' }, false, false)); | |
corpServices.factory('UpdateEventService', newService(serviceRootPath + 'UpdateEvent/:id', { id: '@id' }, false, true)); | |
//#endregion | |
//#endregion | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment