1.0.2 • Published 8 years ago

cacheejs v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

cachee

alt travis ci Code Climate Issue Count HitCount

Cachee is a simple library that helps developer cache resources and requests and reuse them during a long lived application. That way if the app looses connectivity during usage the user will not experience any problems

Install

bower install --save cachee

Usage

<!-- Format: -->
<!-- <tag-name cachee="<attr>:<resource-url>"></tag-name> -->
<img cachee="src:/my-resource1" />
<img cachee="src:/my-resource2" />
<img cachee="src:/my-resource3" />
 
<script>
  cachee.cache([
      cachee.resource('/my-resource1'),
      cachee.resource('/my-resource2'),
      cachee.resource('/my-resource3')
  ]).then(function() {
      //resources loaded
      cachee.load(); or cache.load(myElem);
  });
</script>

Documentation

cachee : object

A namespace that holds methods to help cache data inside blob urls

Kind: global namespace

cachee.cache(cacheRequests) ⇒ Promise

cache resource requests

Kind: static method of cachee

ParamTypeDescription
cacheRequestsArraylist of chache requests

Example

<!-- Format: -->
<!-- <tag-name cachee="<attr>:<resource-url>"></tag-name> -->
<img cachee="src:/my-resource1" />
<img cachee="src:/my-resource2" />
<img cachee="src:/my-resource3" />
<img cachee="src:custom-resource" />

<script>
 cachee.cache([
     cachee.resource('/my-resource1'),
     cachee.resource('/my-resource2'),
     cachee.resource('/my-resource3'),
     cachee.writeResource('custom-resource', 'Custom Content', 'image/png')
 ]).then(function() {
     //resources loaded
     cachee.load(); or cache.load(myElem);
     // => <img src="blob:http://...1" />
     // => <img src="blob:http://...2" />
     // => <img src="blob:http://...3" />
     // => <img src="blob:http://...4" />
 });
</script>

<!-- Using deep attribute set and template attribute -->
<!-- Format -->
<!-- <tag-name cachee-tpl="my url resource ${<attr>}" cachee="<attr>:<resource-url"></tag-name> -->
<div cachee="style.backgroundImage:/my-resource1" cachee-tpl="url(${style.backgroundImage})"></div>

<script>
 cachee.cache([
     cachee.resource('/my-resource')
 ]).then(function() { 
     cachee.load(); // => <div style="background-image: url('blob:http://...')" cachee-tpl="url(${style.backgroundImage})"></div>
 });
</script>

cachee.load(elem)

load specfic resources

Kind: static method of cachee

ParamTypeDefaultDescription
elemElementdocumentroot element or document

cachee.request(opt) ⇒ Promise

send an http request

Kind: static method of cachee

ParamType
optObject

Example

//get request
cachee.request({
 url: '/example/resource',
 method: 'GET',
 responseType: 'arraybuffer'
}).then(function(xhr) {
 console.log(xhr.response);
});

// post request
cachee.request({
 url: '/my-resource',
 method: 'POST',
 data: FormData,
 headers: {
     'Content-type': 'multipart/form-data'
 }
}).then(handle);

cachee.resource(url, opt) ⇒ Promise

Sends a resource request and if the resource is not in cache it will cache it and retun it's blob url

Kind: static method of cachee

ParamTypeDescription
urlStringresource url
optObjecthttp request options

cachee.readResource(url, type) ⇒ Promise

Reads a resource and returns a specific type

Kind: static method of cachee

ParamTypeDefaultDescription
urlStringthe resource url
typeString"text"http compliant responseType string

Example

cachee.readResource('/my-resource', 'arraybuffer').then(function(data) {
 console.log(data);
});

cachee.writeResource(id, content, mimeType) ⇒ Promise

Write a resource to the cache table

Kind: static method of cachee
Returns: Promise - the blob url in a promise

ParamTypeDefaultDescription
idStringunique identifyer
contentMixedthe resource content
mimeTypeString"text/plain"the resource's mime type

Example

cachee.writeResource('hello-world', 'Hello, World', 'text/plain');

// reading the resource

cachee.readResource('hello-world', 'text').then(function(data) { 
 console.log(data); // => "Hello, World"
});

cachee.deleteResource(id)

removes a resource form cache

Kind: static method of cachee

ParamType
idString

Example

cachee.writeResource('hello-world', 'Hello, World', 'text/plain');
...
// delete the resource after we are done with it
cachee.deleteResource('hello-world');

cachee.$$(context, selector) ⇒ Array

Dom helper method

Kind: static method of cachee

ParamType
contextElement
selectorString

Example

cachee.$$(myElement, '.item-selected').forEach(...);

Contribution

Pull requests, Bug reports and feature requests