Last active
December 29, 2015 18:19
-
-
Save amatiasq/7709598 to your computer and use it in GitHub Desktop.
Two AngularJS directives to manage appcache updates.
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
'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(); | |
}); | |
} | |
}; | |
}) | |
; |
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
<!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