Last active
August 29, 2015 13:56
-
-
Save mackstar/9060957 to your computer and use it in GitHub Desktop.
Angular Setup
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
'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