Search⌘ K
AI Features

Proxying the Constructor

Explore how to create a function that proxies a constructor for local storage use in Marionette.js. Understand the process of replacing constructors and safely accessing references to enable data persistence in client-side applications.

With these basics in place, here’s what we want to have a function that will receive a reference to a constructor, e.g., the Contact constructor, and modify it for use with local storage.

Creating a new constructor

So, let’s create a configureStorage function that will do just that:

Node.js
Entities.configureStorage = function(constructor){
var OldConstructor = constructor;
var NewConstructor = function(){
var obj = new OldConstructor(arguments[0], arguments[1]);
return obj;
}
NewConstructor.prototype = OldConstructor.prototype;
};

Our configureStorage function takes a constructor as an argument in line 1, assigns it to the OldConstructor variable for readability in line 2, and then defines a new constructor in lines 3–6. This ...