GithubHelp home page GithubHelp logo

fearphage / backbone.cacheit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tbranyen/backbone.cacheit

0.0 2.0 0.0 155 KB

Fetch caching made super simple in Backbone.

License: MIT License

JavaScript 100.00%

backbone.cacheit's Introduction

backbone.cacheit v0.0.1

Created by Tim Branyen @tbranyen.

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.

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.0.1

  • Initial beta release.

backbone.cacheit's People

Contributors

tbranyen avatar

Watchers

 avatar  avatar

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.