JSFiddle - React, Tailwind, and code Playground
by GrantGamesGames
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/restangular/1.4.0/restangular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<div ng-app="myApp">
<div>Status: {{status}}</div>
<div>Error: {{error}}</div>
<div>Result: {{data}}</div>
</div>
JavaScript
var myApp = angular.module('myApp', ['restangular']);
myApp.config(function(RestangularProvider){
RestangularProvider.setBaseUrl('http://jsonplaceholder.typicode.com');
});
myApp.run(function(Restangular, $q, $rootScope){
Restangular.addFullRequestInterceptor(_checkCache);
Restangular.one('posts', 1).get()
.then(function(result) {
$rootScope.data = result;
}, function(err) {
$rootScope.error = err;
});
function _checkCache(element, operation, route, url, headers, params, httpConfig) {
if(operation === 'get') {
//Check the cache to see if the resource is already cached
var data = {data: 'the cached data'};
//If cache object does exist, return it
if(data !== undefined) {
angular.extend(element, data);
var defer = $q.defer();
defer.reject();
httpConfig.timeout = defer;
return {
element: element,
headers: headers,
params: params,
httpConfig: httpConfig
};
}
}
}
});