GithubHelp home page GithubHelp logo

gerhobbelt / backbone-singleton Goto Github PK

View Code? Open in Web Editor NEW

This project forked from reconbot/backbone-singleton

0.0 1.0 0.0 7.65 MB

An implementation of a singleton factory pattern for backbone objects.

backbone-singleton's Introduction

Backbone Singleton

Backbone singleton can be used to keep a cache of all your models. If new models are created that are the same as your existing models you'll get a reference to your origional model and it's data will be updated. This allows for some cool patterns but comes with few caveats with long lived pages as your models will never be garbage collected without some intervention.

Backbone singleton is tested with backbone's own model tests. It passes. It is also tested with a small suite of tests to ensure the singleton pattern. It passes that too.

To use me - just extend me

(not required but it's recommended)
  var CoolModel = Backbone.SingletonModel.extend({});
  var a = new CoolModel({id: 4, coolLevel: "low"});
  var b = new CoolModel({id: 4, coolLevel: "MAX"});
  console.log(a === b); // true!
  console.log(a.get('coolLevel')); // "MAX"

Inspiration

API

We define three instance methods

  • model.capture()

    • Submits a free model to the model store
    • Updates a model that's already in the store (why would you do this?)
    • Increments the stores reference count of the model
  • model.release()

    • Used to tell the store you're done with the model
    • Decrements the stores reference count of the model
  • model.hash()

    • Returns the id
    • Respects the idAttribute
    • Should be unique per Model class
    • Can be overidden if you need more specificality than the id of the model

We have two static/class methods

  • Backbone.SingletonModel.extend()
    • Does what you think it should
    • It is patched to give each subclass its own singleton store
  • Backbone.SingletonModel.reap()
    • Deletes all models who's referece count is below 1

We have two properties

  • model.singleton - Boolean()
    • True if the model is in the store
    • False if not - this happens when hash() returns undef (eg, no id)
  • model._singleton - {store:{}, count:{}}
    • A reference to the store that's on the Constructor object.
    • Cool to look at

##TODO:

  • Test custom Hash's

backbone-singleton's People

Contributors

koudelka avatar reconbot avatar

Watchers

 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.