A store for keeping unique instances of Backbone models with ease.
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'; // trueStore will also update the attributes of the instance to reflect the latest state.
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); // trueIf you need additional control, Store exposes additional API and events.
Store contains only static methods as has no instance methods or constructor.
Returns a Stored Model defintion.
If modelName is not given a unique generate name will be used.
ModelCache definition used internally.
Useful for overriding and extending.
Adds a ModelCache for the Model definition by modelName to the store.
Returns the ModelCache for the Model definition.
Returns a ModelCache by name.
Returns all ModelCache instances by name.
Returns a Model definition by name.
Returns all Model definitions by name.
Removes a ModelCache from Store by name.
Removes the entire cache.
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);
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 });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' });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 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'sModelModel- the original model defintion of the cache.modelName- the name the cache is stored as on theStore.ModelConstructor- the unique model definition return by the store.
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.
Removes the model instance from the cache.
Backbone.Store is heavily inspired by Backbone.UniqueModel written by Ben Vinegar
===
This library is © 2020 RoundingWell. Distributed under MIT license.