constructor-hydrate
Check the instanceStore when creating new instances of the connected
Map type. Return an existing instance if a match is found. Prevents duplication of
instances when instances are created outside of the can-connect connection.
constructorHydrate( baseConnection )
Overrides DefineMaps's setup method and checks whether a newly created instance already
exists in the instanceStore. If an instance exists in the
instanceStore that instance will be returned instead of a new object.
This behavior expects to be used with the constructor/store and can/map behaviors.
Parameters
- baseConnection
{Object}:can-connectconnection object that is having theconstructor-hydratebehavior added on to it.
Returns
{Object}:
a can-connect connection containing the methods provided by constructor-hydrate.
Use
During all initializations of the connection Map type, if an identity property exists then
the constructor-hydrate behavior will check the connection instanceStore for
a matching instance. If one is found it will be returned instead of a new object.
For example, this behavior is useful if Type converters of
DefineMap are used in your app.
Let's say we have a page state type, two properties of which are of type Student:
var myPage = new (DefineMap.extend({
student: { Type: Student },
teamLead: { Type: Student },
loadTeamLead: function() {...}
}));
The type Student is a DefineMap that has had a can-connect connection attached:
var dataUrl = require("can-connect/data/url/");
var constructor = require("can-connect/constructor/contructor");
var store = require("can-connect/constructor/store/store");
var dataUrl = require("can-connect/data/url/url");
var canMap = require("can-connect/can/map/map");
var constructorHydrate = require("can-connect/can/constructor-hydrate/constructor-hydrate");
var Student = DefineMap.extend({...});
Student.List = DefineList.extend({
'#': { Type: Student }
});
Student.connection = connect(
[dataUrl, constructor, store, canMap, constructorHydrate, dataUrl], {
Map: Student,
List: Student.List,
url: "api/students"
}
);
Now lets say your page loads myPage.student via the connection using Student.get(), and then it gets data for
teamLead without the connection via myPage.loadTeamLead(). In this example it so happens that the team lead is
the same person as the student:
Student.get({id: 1}).then(function(person) {
// loaded via can-connect, person.id === 1
myPage.student = person;
// not loaded via can-connect, person.id === 1
myPage.loadTeamLead(myPage.student.teamId).then(function(person){ myPage.teamLead = person; });
});
Without constructor-hydrate we would end up with two different instances of Student with the same id. Additionally,
teamLead would not be an instance that is stored in the connection's instanceStore, and thus would not benefit
from the real-time updates offered by the real-time behavior.
constructor-hydrate solves this problem by checking instanceStore before creating a new instance. So, in our
example it will return the existing instance from myPage.loadTeamLead(). Now both myPage.student and
myPage.teamLead are referencing the same instance:
var instanceStore = Student.connection.instanceStore;
myPage.student === myPage.teamLead; // => true
myPage.teamLead === instanceStore.get( myPage.teamLead.id ); // => true