Code Monkey home page Code Monkey logo

ng-appcache's Introduction

ng-appcache

Build Status

ng-appcache is an AngularJS module for working with the application cache. The module offers both a service for appcache interaction and utility directives for all states. ng-appcache fails silently if a browser does not have appcache support, so it is safe to use, and the implementation is fully testable by mocking the $window service.

ng-appcache aims to work wherever the appcache is available, so please file an issue if you run into problems.


## Installation
  1. Look in the dist subfolder, or if you have bower installed (if you don't, you probably want to) just run
$ bower install ng-appcache
  1. Include the downloaded file in your base html file
<script src="path/to/appcache.js"></script>
<!-- or -->
<script src="path/to/appcache.min.js"></script>
  1. And finally include the module as a dependency for your angular app:
angular.module('myApp', ['ng-appcache']);

## Service API

Include appcache as a dependency for your angular component, eg.

angular.controller('MyCtrl', function(appcache) {

  appcache.checkUpdate().then(function() {
    alert('There\'s an update available!');
  });
  // ...
});

The service offers the following API:
appcache.abortUpdate()

Abort an ongoing appcache download. If an appcache update is ongoing and is successfully canceled, the function will return true, in all other cases this function will return false and have no other effect.


##### appcache.checkUpdate() Manually check if a new application cache is available, and automatically download it if so. This function returns a ```$q``` promise, which will be resolved when the update is ready to be applied, or will be rejected if no update is available.

N.B. browsers usually automatically check for updates when pages are (re-)loaded, so calling this function immediately on pageload is probably unnecessary.


##### appcache.swapCache() If an appcache update is already downloaded, after a call to this method any requests for cached content will return new versions instead of the ones available from the cache. This function returns a ```$q``` promise that will be rejected if no cache is available, or resolved otherwise (success).

N.B. the internet suggests that it is probably safer to do a full pageload when you have an appcache update downloaded, so that there are no mixed resource versions in use.


##### appcache.addEventListener(eventName, handler, useCapture) ##### appcache.on(...) Register a function _handler_ which will be executed when _eventName_ fires. See [this reference](http://www.quirksmode.org/js/events_order.html) for an explanation of the _useCapture_ parameter.

eventName can be one of 'cached', 'checking', 'downloading', 'error', 'noupdate', 'obsolete', 'progress' or 'updateready'.


##### appcache.removeEventListener(eventName, handler, useCapture) ##### appcache.off(...) Remove function _handler_ previously registered to be executed when _eventName_ fires. _handler_ must refer to a named function or a stored variable containing a function, otherwise this call will have no effect.
##### appcache.textStatus A string representation of the current appcache state. ```appcache.textStatus``` will be one of ```'UNCACHED', 'IDLE', 'CHECKING', 'DOWNLOADING', 'UPDATEREADY'``` or ```'OBSOLETE'```.
appcache.status

A numerical representation of the current appcache state which matches the constants provided by window.applicationCache.status and is expressed textually by appcache.textStatus.


### Unsupported browsers In the case of a browser that does not support appcache, calls to
appcache.abortUpdate()

will return false

appcache.checkUpdate()
appcache.swapCache()

will return a rejected promise,

appcache.addEventListener()
appcache.removeEventListener()
appcache.on()
appcache.off()

will have no effect, and

appcache.status and
appcache.textStatus

will be undefined.


## Directives

With ng-appcache installed, you can use the following directives:

<appcache-cached>
<appcache-checking>
<appcache-downloading>
<appcache-error>
<appcache-noupdate>
<appcache-obsolete>
<appcache-updateready>

All directives can also be specified as attributes, eg <div appcache-noupdate>. Any content inside the directives will initially hidden (using the ng-hide class), and become visible if the corresponding appcache event fires.

By adding the dismiss-on-click or dismiss-delay (or both) attributes to any directive, the directive content can be hidden again. dismiss-on-click will hide content on a mouseclick, and dismiss-delay takes a millisecond parameter which specifies the delay before the content will be hidden again.

Example:

<appcache-error dismiss-on-click dismiss-delay="2500"></appcache-error>

The example directive will initially be hidden, and become visible if an appcache error event occurs. 2,5 seconds later the content will be hidden, or before that if a user clicks the directive.


## Application cache resources

The appcache can be a douchebag, so have a look at some of the following helpful articles if you're not already familiar with the details:


## Developing

ng-appcache has been developed using Gulp, with a minimal CI setup. To get started, run

$ npm install

... which will set up a development environment with PhantomJS for automated testing and a server for manual testing on other browsers. After this, the following gulp tasks are available:

  • gulp build: copy and minify source file into the dist folder
  • gulp test: run all tests once against the dist folder contents
  • gulp develop: automatically run build task and jshint reporting when source file is saved, run tests when test files are saved or the dist folder content changes

Because of how the appcache works, you will need to clear the browser cache whenever changing filenames or including new files in the ```index-with-appcache.html``` file.
  • to clear the cache, temporarily move or rename the test/test.appcache file and run gulp test once. When PhantomJS requests the manifest file, it will get a 404 response and delete the cache.
  • alternatively, see the guide here for instructions on how to clear the cache on desktop browsers.

PhantomJS does not currently offer any alternatives to the first approach (moving the manifest file), so changes are a bit cumbersome.


## License

Everything is covered by the MIT license, so wear it, share it, tear it and throw it away.

ng-appcache's People

Contributors

orbitbot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ng-appcache's Issues

To use ServiceWorker

AppCache is an old API.
Firefox recommend to update it.
Here is console output.

The Application Cache API (AppCache) is deprecated and will be removed at a future date. Please consider using ServiceWorker for offline support.

bower does not supply minified version

Hi

I just noticed, that when I run
bower install ng-appcache
I am just supplied with the normal, but not the minified version. Is this intended?

regards

Andreas

bower.json specifies minified and unminified version

We use wiredep to automatically include Bower dependencies in our HTML. With ng-appcache this includes both dist/appcache.js and dist/appcache.min.js since they're listed under the main key in bower.json.

Could you change this to only specify one or the other? I don't particularly care which since we'll minify it again anyway :)

Should directives and attribute names be shortened?

Both the provided directive names (appcache-updateready ...) and configuration attributes (dismiss-on-click, dismiss-delay) are fairly long, but should convey their meaning fairly well. However, these could be shortened to something less verbose if suitable alternatives can be found. What should shorter names for these be?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.