Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active December 29, 2015 18:19
Show Gist options
  • Save amatiasq/7709598 to your computer and use it in GitHub Desktop.
Save amatiasq/7709598 to your computer and use it in GitHub Desktop.
Two AngularJS directives to manage appcache updates.
'use strict';
angular.module('mq-appcache', [])
.directive('manifest', function() {
return {
compile: function() {
if (window.applicationCache) {
applicationCache.addEventListener('updateready', function() {
console.log('New version downloaded, reload to see the changes.');
});
}
}
};
})
.directive('mqAppcacheUpdate', function($window, $document) {
return {
restrict: 'EA',
compile: function(elem) {
elem.addClass('ng-hide');
if (window.applicationCache) {
applicationCache.addEventListener('updateready', function() {
elem.removeClass('ng-hide');
$document.find('body').addClass('mq-appcache-update');
});
}
elem.on('click', function() {
$window.location.reload();
});
}
};
})
;
<!DOCTYPE html>
<html manifest="my-manifest.appcache" ng-app="mq-appcache">
<head>
<title>Appcache update test</title>
<script type="text/javascript" src="mq-appcache.js"></script>
<script type="text/javascript">
angular.module('my-app', [ 'mq-appcache' ])
.controller('MainCtrl', function($scope, $window) {
$scope.reload = function() {
$window.location.reload();
};
})
</script>
</head>
<body ng-controller="MainCtrl">
<mq-appcache-update>
A new version is available. Please <span ng-click="reload()">reload the page</span> to update.
</mq-appcache-update>
<!-- More page content -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment