GithubHelp home page GithubHelp logo

tbranyen / backbone.cacheit Goto Github PK

View Code? Open in Web Editor NEW
47.0 6.0 9.0 385 KB

Fetch caching made super simple in Backbone.

License: MIT License

CoffeeScript 1.54% JavaScript 88.17% HTML 1.69% CSS 8.61%

backbone.cacheit's Introduction

backbone.cacheit

Fetch caching made super simple in Backbone.

v0.1.0 Build Status

Maintained by Tim Branyen @tbranyen with help from awesome contributors!

This is useful for when you have Models and Collections that once data is fetched you can operate with the same dataset throughout the lifetime of your application. Whatever route your application starts with can safely call the fetch method to get the data into the instances.

Tested with Underscore, Backbone and jQuery. You can swap out jQuery with a custom configuration or substitute Underscore with Lo-Dash.

Getting started

Include into existing Backbone application:

<!-- Optional dependencies. -->
<script src="jquery.js"></script>
<script src="underscore.js"></script>
<script src="backbone.js"></script>

<!-- Load the Backbone.cacheit plugin library. -->
<script src="backbone.cacheit.js"></script>

Compatibility: Everything Backbone and jQuery supports? Let me know if you find issues.

Documentation

Every fetch method is patched to return a Deferred, instead of the jqXHR object in jQuery.ajax. If you call fetch multiple times while it is fetching or after it's fetched, you will always get the same Deferred and it will not make additional requests.

Cancelling is super simple, just add the reload option:

fetch({ reload: true });

optionally you can make a Model or Collection always reload by doing the following (works with Models too):

Backbone.Collection.extend({
  reload: true
}):

Since you're getting a brand new deferred and nothing ever happens to the old one you can be assured that fetch callbacks will only trigger after the most recent fetch has completed.

If you want use underscore.deferred or some other implementation you can specify an override to swap out that dependency.

Backbone.Collection.prototype.fetch.deferred = function() {
  return _.Deferred();
};

Notes on SafeSync

It's entirely possible that requests will come back out of order if you use the { reload: true } option. This is most likely not your intention. If you agree, you should put the following plugin into your project. CacheIt will work seamlessly.

https://github.com/amccloud/backbone-safesync

Release notes

0.1.0-pre

  • Bringing it to the tbranyen project standard. Just added a bunch of files and compatibility with AMD, Grunt 0.4, etc.
  • Integrated a fix by @LoonyPandora to reject the deferred if the request fails.

0.0.1

  • Initial beta release.

backbone.cacheit's People

Contributors

loonypandora avatar tbranyen avatar twalker avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

backbone.cacheit's Issues

Add a timeout config setting

Hey T!

I used to add a getModel & getCollection method to my "repositories", that would cache the results, and immediately invoke the options.success function if the collection or model was already loaded.

Now I am refactoring my code to make use of the deferreds, and landed on your caching library.

However, in my code I added a little "timer" that would make sure no requests would be made if the data that was already loaded is newer than X minutes.

I would love to see this feature in your library =)

Version 0.2 Feature Requests

I'm thinking this plugin needs a lot more love and features.

  • Local storage caching.
  • Partial updates (don't need to refresh an entirely new dataset).
  • Status properties and events (how and where the data was loaded).

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.