Skip to content

Instantly share code, notes, and snippets.

@mackstar
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackstar/9060957 to your computer and use it in GitHub Desktop.
Save mackstar/9060957 to your computer and use it in GitHub Desktop.
Angular Setup
'use strict';
var app = angular.module('spout', ['ngRoute', 'restangular', 'ngAnimate', 'ui.bootstrap']);
/* Utility service to apply form errors to a form */
app.service('parseFormErrors', function() {
return function (data, form) {
for(property in data.errors) {
form[property].$dirty = true;
form[property].$invalid = true;
form[property].$message = data.errors[property];
}
}
});
app.run(function(Restangular, $rootScope) {
/* Restful API Route */
Restangular.setBaseUrl('/api');
/* Standard Response Interceptor */
Restangular.setResponseInterceptor(function(data, operation, what, request, response) {
if (data.message) {
$rootScope.$emit('message', {message: data.message});
}
if (data._model && data._pager) {
data[data._model]._pager = data._pager;
}
if (data._model) {
return data[data._model];
}
return data;
});
/* Error Interceptor */
Restangular.setErrorInterceptor(function(response) {
if (response.status && response.data.title) {
$rootScope.$emit('sp.message',
{
title: response.data.title,
message: response.data.message,
type: "danger"
}
);
}
return response;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment