>
> Here is an example of a data access service that wraps (in this case
> $http). The idea being you can funnel all of you web api calls through a
> single service and maintain the interface from here.
>
(function (window, document, angular) {
'use strict';
/**
* a data access layer service, sits between the angular application,
and the web api
*
* @constructor
* @param {object} http - angular http
*/
function DataAccessService(http) {
this.http = http;
}
/*
* request a report from the web api
*
* param {string} url - the url to call
* param {function} successFn - a callback function upon success
* param {function} successFn - a callback function upon success
* param {object} context - the context of the calling service (this of
the caller)
*/
DataAccessService.prototype.get = function (url, successFn, errorFn,
context) {
this.http({ method: 'GET', url: url })
.success(function (data, status, headers, config) {
successFn(data, context);
})
.error(function (data, status, headers, config) {
if (data === undefined || data === null ||
!data.hasOwnProperty('message')) {
errorFn('server error', context);
} else {
errorFn(data.message, context);
}
return;
});
return true;
};
/*****************************************************************
* ANGULAR EXPOSED SERVICE
******************************************************************/
angular.module('app').factory('MyDataAccess', ['$http', function
($http) {
return new DataAccessService($http);
})();
})(window, document, angular);
You can also consider a controller action like:
[HttpGet]
public HttpResponseMessage Get()
{
try
{
// todo: replace with a service method
var viewModel = new[] { "value1", "value2" };
return Request.CreateResponse(HttpStatusCode.OK, viewModel);
}
catch (Exception ex)
{
// todo: replace with some user friendly error message
return
Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.