Skip to content

Instantly share code, notes, and snippets.

@itswadesh
Last active March 27, 2017 15:17
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 itswadesh/1b147d43979eb0eba8686b81222e4a58 to your computer and use it in GitHub Desktop.
Save itswadesh/1b147d43979eb0eba8686b81222e4a58 to your computer and use it in GitHub Desktop.
var app = angular.module('myApp', ['ngResource', 'ngProgress', 'ngAnimate', 'toaster']);
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('myHttpInterceptor');
});
// Handle http server errors
app.factory('myHttpInterceptor', function ($q,toaster) {
return {
responseError: function (response) {
console.log(response);
if(response.data){
if(response.data.message)
toaster.error("Error: ", response.data.message);
else
toaster.error("Error: ", response.data);
}
return $q.reject(response);
}
};
});
// Showing loading indicator on top while data is requested from database
app.directive('loading', ['$http', 'ngProgress', function ($http, ngProgress)
{
return {
restrict: 'A',
link: function (scope, elm, attrs)
{
scope.isLoading = function () {
return $http.pendingRequests.length > 0;
};
scope.$watch(scope.isLoading, function (v)
{
if(v){
ngProgress.start();
}else{
ngProgress.complete();
}
});
}
};
}]);
// Create a resource factory to access products table from database
app.factory('Product', function($resource) {
return $resource('/api/products/:id', { id: '@_id' }, {
update: { // We need to define this method manually as it is not provided with ng-resource
method: 'PUT'
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment