Search⌘ K
AI Features

Determining the Storage Key

Explore how to determine and manage storage keys for localStorage in Marionette.js applications. Understand the use of Underscore's result function for dynamic URLs, and learn to implement a storage cache that ensures all model and collection instances share the same Backbone.LocalStorage instance for reliable data persistence.

Creating a function to determine the storage key

We need to determine the storage key according to each entity, so ...

Node.js
ContactManager.module("Entities", function(Entities, ContactManager,
Backbone, Marionette, $, _){
var findStorageKey = function(entity){
// use a model's urlRoot value
if(entity.urlRoot){
return entity.urlRoot;
}
// use a collection's url value
if(entity.url){
return entity.url;
}
throw new Error("Unable to determine storage key");
};
var StorageMixin = function(entityPrototype){
var storageKey = findStorageKey(entityPrototype);
return { localStorage: new Backbone.LocalStorage(storageKey) };
};
var getEntity = function(constructorString){
// code truncated for brevity
};
Entities.configureStorage = function(constructorString){
// code truncated for brevity
};
});
...