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

rwell-backbone.uniquemodel

v1.0.1

Published

Backbone.UniqueModel ensures unique model instances across your application.

Downloads

1

Readme

Backbone.UniqueModel

Backbone.UniqueModel ensures unique model instances across your application. It will also synchronize model data between windows/iframes using localStorage.

Usage

Instantiating models

When creating a new model, if that model is already being tracked, you will be returned the original model instance.

var UniqueUser = Backbone.UniqueModel(User);

var first  = new UniqueUser({ id: 1, name: 'Jean Grey' });
var second = new UniqueUser({ id: 1, name: 'Jean Summers' });

first === second; // true
first.get('name') === 'Jean Summers'; // true

UniqueModel will also update the attributes of the instance to reflect the latest state.

Working with collections

Backbone.UniqueModel also guarantees that instances created through a collection (e.g. via fetch) are also unique.

var UserCollection = Backbone.Collection.extend({
  model: UniqueUser
});

var users = new UserCollection([
  { id: 2, name: 'Henry McCoy' },
  { id: 3, name: 'Bobby Drake' }
]);

var user = new UniqueUser({ id: 2, name: 'Henry McCoy' });
user === users.get(2); // true

Window sync via localStorage

If enabled, UniqueModel will attempt to ensure uniqueness of model instances across windows using localStorage.

// Window 1
var UniqueUser = Backbone.UniqueModel(User, 'User', 'localStorage');
var logan1 = new UniqueUser({ id: 4, name: 'Logan' });

// Window 2
var UniqueUser = Backbone.UniqueModel(User, 'User', 'localStorage');
var logan2 = new UniqueUser({ id: 4, name: 'Logan', power: 'Healing' });

// Back to Window 1
logan1.get('power'); // Healing

Add event

It's possible for completely new models to become available through localStorage sync. To be notified of new models, subscribe to the uniquemodel.add event on your UniqueModel class.

For example, you can use this event to automatically add new models to your collections:

UniqueUser.on('uniquemodel.add', function (model) {
  userCollection.add(model);
});

Destroy event

If a model is destroyed in one window, the destroy event will be called on that model in any other open windows.

In Backbone, collections automatically remove any models that trigger destroy events. So there's nothing for you to do here — just know that it happens automatically.

// Window 1
userCollection.add(logan1);

// Window 2
logan2.destroy(); // Triggers 'destroy' event in Window 1

// Back to Window 1
userCollection.where({ name: 'Logan' }).length === 0; // Removed from set

Demo

Bundled in this repository is a version of TodoMVC that has been modified to use UniqueModel. It's a good demonstration of UniqueModel's window syncing abilities. Open up the demo in multiple windows, and observe your changes propagate instantly between each window intance.

You can try the demo live on GitHub, or you can run it yourself from the repository.

If you're curious, this demo was done by adding only 2 lines of code:

  • Making Todo as a UniqueModel class (link)
  • Binding the TodoCollection to the uniquemodel.add event (link)

Running Tests

The unit tests need to be served from a web server; they cannot be accessed from local filesystem. This is because localStorage isn't shared between file:// resources in Chrome, which causes the sync tests to fail in that browser.

If you have Python installed (pre-installed on OS X), you can just use SimpleHTTPServer:

$ python -m SimpleHTTPServer 8000

Then open http://localhost:8000/tests/ and you're off to the races.

Browser support

UniqueModel has been verified working in the following browsers:

  • Internet Explorer 8+ (localStorage sync requires IE9+)
  • Chrome 25
  • Safari 6
  • Firefox 18

IE8 and localStorage sync

Despite implementing webstorage, IE8's onstorage event doesn't communicate what data changed. There are workarounds, which I plan to explore in a future version.

Acknowledgments

Backbone.UniqueModel is written by Ben Vinegar, based on work from Anton Kovalyov and Burak Yigit Kaya.