Determining the Storage Key

Understand which storage key would be feasible according to the requirements of the entities.

Creating a function to determine the storage key

We need to determine the storage key according to each entity, so let’s create a function for that:

Press + to interact
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
};
});
...