npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-cached-resource

v0.3.2

Published

Work with $resources offline using localStorage!

Downloads

14

Readme

ngCachedResource NPM version Build Status

An AngularJS module to interact with RESTful server-side data sources, even when the browser is offline. Uses HTML5 localStorage under the hood. Closely mimics the behavior of the core ngResource module.

A simple example

var Article = $cachedResource('article', '/articles/:id');

// GET requests:
var firstArticle = Article.get({id: 1});
firstArticle.$promise.then(function() {
  // firstArticle came from localStorage, if it has been loaded before
});
firstArticle.$httpPromise.then(function() {
  // Even if firstArticle was loaded before, now it has been fully refreshed
  // and represents the response from the /articles/1 endpoint
});

// POST/PUT/DELETE requests:

// If this fails initially, possibly because of a bad connection, we will
// try sending it again
Article.save({id: 2}, {contents: "Lorem ipsum dolor..."}, function() {
  // Reaching this callback means it successfully saved.
});

Usage

Provides a factory called $cachedResource that returns a "CachedResource" object.

$cachedResource(cacheKey, url, [paramDefaults], [actions]);

Arguments

  • cacheKey, String An arbitrary key that will uniquely represent this resource in localStorage. When the resource is instanciated, it will check localStorage for any

  • url, String Exactly matches the API for the url param of the $resource factory.

  • paramDefaults, Object, (optional) Exactly matches the API for the paramDefaults param of the $resource factory.

  • actions, Object, optional Exactly matches the API for the actions param of the $resource factory.

Returns

A CachedResource "class" object. This object is basically a swap-in replacement for an object created by the $resource factory with the following modified or additional properties:

  • Resource.$promise: For GET requests, if anything was already in the cache, this promise is immediately resolved (still asynchronously!) even as the HTTP request continues.

  • Resource.$httpPromise: For all requests, this promise is resolved as soon as the corresponding HTTP request responds.


Details

Asking for a cached resource with get or query will do the following:

  1. If the request has not been made previously, it will immediately return a resource object, just like usual. The request will go through to the server, and when the server responds, the resource will be saved in a localStorage cache.

  2. If the request has already been made, it will immediately return a resource object that is pre-populated from the cache. The request will still attempt to go through to the server, and if the server responds, the cache entry will be updated.

Updating a CachedResource object will do the following:

  1. Add the resource update action to a queue.
  2. Immediately attempt to flush the queue by sending all the network requests in the queue.
  3. If a queued network request succeeds, remove it from the queue and resolve the promises on the associated resources (only if the queue entry was made after the page was loaded)
  4. If the queue contains requests, attempt to flush it once per minute OR whenever the browser sends a navigator.onOnline event.