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

backbone.store

v1.1.1

Published

Store for keeping unique instance of Backbone models with ease.

Downloads

60

Readme

Backbone.Store

A store for keeping unique instances of Backbone models with ease.

Usage

Instantiating models

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

var StoredUser = Backbone.Store(User);

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

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

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

Working with collections

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

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

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

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

API

If you need additional control, Store exposes additional API and events.

Store

Store contains only static methods as has no instance methods or constructor.

Store(Model, [modelName])

Returns a Stored Model defintion. If modelName is not given a unique generate name will be used.

Store.ModelCache

ModelCache definition used internally. Useful for overriding and extending.

Store.add(Model, modelName)

Adds a ModelCache for the Model definition by modelName to the store. Returns the ModelCache for the Model definition.

Store.getCache(modelName)

Returns a ModelCache by name.

Store.getAllCache()

Returns all ModelCache instances by name.

Store.get(modelName)

Returns a Model definition by name.

Store.getAll()

Returns all Model definitions by name.

Store.remove(modelName)

Removes a ModelCache from Store by name.

Store.removeAll()

Removes the entire cache.

Store examples

import Store from `backbone.store`;

const StoredModel = Store(Backbone.Model, 'myModel');

const StoredMyModel = Store.get('myModel');

console.log(StoredModel === StoredMyModel); // true

const MyModelCache = Store.getCache('myModel');

console.log(StoredModel ===  MyModelCache.ModelConstructor); // true

const Models = Store.getAll();

console.log(StoredModel === Models.myModel.modelConstructor);

Store Events

add

Triggered when a new instance is added to a ModelCache This event gets the instance and the ModelCache instance passed to it.

// Clean cache
Store.removeAll();

Store.on('add', function(instance, ModelCache) {
  console.log('model added');
});

// logs "model added"
new StoredUser({ id: 1 });

const newUser = new StoredUser();

// logs "model added"
newUser.set({ id: 5 });

update

Triggered when a cached instance is returned. Any data instantiated updates the cached instance. This event gets the instance and the ModelCache instance passed to it.

// Clean cache
Store.removeAll();

Store.on('update', function(instance, ModelCache) {
  console.log('model updated: ' + instance.get('name'));
});

const user = new StoredUser({ id: 1 });

// logs "model updated: bob"
const user2 = new StoredUser({ id: 1, name: 'bob' });

remove

Triggered when an instance is removed from a ModelCache. This event gets the instance and the ModelCache instance passed to it.

// Clean cache
Store.removeAll();

Store.on('remove', function(instance, ModelCache) {
  console.log('model removed');
});

const user = new StoredUser({ id: 1 });

// logs "model removed"
user.destroy();

ModelCache

ModelCache instances will be created for each Model added to the Store. When a model instance is destroyed it will be removed automatically.

Instances will have four properties:

  • instances - an object with each instance of the cache's Model
  • Model - the original model defintion of the cache.
  • modelName - the name the cache is stored as on the Store.
  • ModelConstructor - the unique model definition return by the store.

get(attrs, options)

If the instance by index is not cached it is instantiated, cached, and returned. Otherwise it returns the cached instance and sets the attrs on the model. The options passed to this method will pass through to the new or the set.

remove(instance)

Removes the model instance from the cache.

Acknowledgments

Backbone.Store is heavily inspired by Backbone.UniqueModel written by Ben Vinegar

===

This library is © 2020 RoundingWell. Distributed under MIT license.