DoneJS StealJS jQuery++ FuncUnit DocumentJS
6.0.1
5.33.2 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-deep-observable
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-observable-bindings
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-model
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
        • behaviors
          • ./base/
          • ./cache-requests/
          • ./can/constructor-hydrate/
          • ./can/map/
          • ./can/ref/
          • ./can/session/
          • ./can-local-store
          • ./can-memory-store
          • ./constructor/callbacks-once/
          • ./constructor/
          • ./constructor/store/
          • ./data/callbacks/
          • ./data/callbacks-cache/
          • ./data/combine-requests/
          • ./data/parse/
          • ./data/url/
          • ./data/worker/
          • ./fall-through-cache/
          • ./real-time/
        • modules
          • ./can/tag/
          • ./helpers/map-deep-merge
          • ./helpers/weak-reference-map
        • data types
          • DataInterface
          • Instance
          • InstanceInterface
          • List
          • ListData
        • deprecated
          • ./can/base-map/
          • ./can/merge/
          • ./can/super-map/
          • ./data/localstorage-cache/
          • ./data/memory-cache/
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-define-realtime-rest-model
      • can-define-rest-model
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

constructor-hydrate

  • Edit on GitHub

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

  1. baseConnection {Object}:

    can-connect connection object that is having the constructor-hydrate behavior 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

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 6.0.1.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news