Last active
March 27, 2017 15:17
-
-
Save itswadesh/1b147d43979eb0eba8686b81222e4a58 to your computer and use it in GitHub Desktop.
This file contains 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 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