DoneJS StealJS jQuery++ FuncUnit DocumentJS
3.14.1
5.0.0 4.3.0 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-compute
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-stream
      • can-define-stream-kefir
      • can-event
      • can-event/async/async
      • can-event/batch/batch
      • can-event/lifecycle/lifecycle
      • can-kefir
      • can-list
      • can-map
      • can-map-backup
      • can-map-define
      • can-observation
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
    • Data Modeling
      • can-connect
        • behaviors
          • ./base/
          • ./cache-requests/
          • ./can/constructor-hydrate/
          • ./can/map/
          • ./can/merge/
          • ./can/ref/
          • ./constructor/callbacks-once/
          • ./constructor/
          • ./constructor/store/
            • stores
              • addInstanceReference
              • addListReference
              • deleteInstanceReference
              • deleteListReference
              • instanceStore
              • listStore
              • moveCreatedInstanceToInstanceStore
            • CRUD callbacks
              • createdInstance
              • updatedList
            • CRUD methods
              • destroy
              • get
              • getList
              • save
            • hydrators
              • hydrateInstance
              • hydrateList
              • hydratedInstance
              • hydratedList
          • ./data/callbacks/
          • ./data/callbacks-cache/
          • ./data/combine-requests/
          • ./data/localstorage-cache/
          • ./data/memory-cache/
          • ./data/parse/
          • ./data/url/
          • ./data/worker/
          • ./fall-through-cache/
          • ./real-time/
        • modules
          • ./can/base-map/
          • ./can/model/
          • ./can/super-map/
          • ./can/tag/
          • ./helpers/map-deep-merge
          • ./helpers/weak-reference-map
        • data types
          • DataInterface
          • Instance
          • InstanceInterface
          • List
          • ListData
      • can-connect-cloneable
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-signalr
      • can-fixture
      • can-fixture-socket
      • can-ndjson-stream
      • can-set
    • Views
      • can-component
      • can-ejs
      • can-element
      • can-react-component
      • can-stache
      • can-stache/helpers/route
      • can-stache-bindings
      • can-stache-converters
      • can-view-autorender
      • can-view-callbacks
      • can-view-href
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • react-view-model
      • react-view-model/component
      • steal-stache
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-globals
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-string
      • can-string-to-any
      • can-util
      • can-zone
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-control
      • can-dom-events
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-jquery
    • Data Validation
      • can-define-validate-validatejs
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-namespace
      • can-reflect
      • 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/store

  • Edit on GitHub

Adds support for keeping references to active lists and instances. Prevents different copies of an instance from being used by the application at once. Allows other behaviors to look up instances currently active in the application.

constructorStore( baseConnection )

Overwrites baseConnection so it contains a store for instances and lists. This behavior:

  • extends the hydrateInstance and hydrateList methods to return instances or lists from the store, if available
  • overwrites "CRUD" methods to make sure that while requests are pending, new lists and instances have references kept in the store. This prevents duplicated instances from being created during concurrent requests.
  • provides methods to add and remove items in the store by counting references

Parameters

  1. baseConnection {Object}:

    can-connect connection object that is having the constructor/store behavior added on to it. Should already contain a behavior that provides the InstanceInteface (e.g constructor). If the connect helper is used to build the connection, the behaviors will automatically be ordered as required.

Returns

{Object}:

a can-connect connection containing the method implementations provided by constructor/store.

Use

The constructor-store behavior is used to:

  • provide a store of instances and lists in use by the client
  • prevent multiple instances from being generated for the same id or multiple lists for the same listSet.

The store provides access to an instance by its id or a list by its listSet. This is used by other behaviors to lookup instances that should have changes applied. Two examples, when there is a new instance that should be added to a list (real-time) or when newer data is available for a cached instance that is used in the page (fall-through-cache).

Below you can see how constructor-store's behavior be used to prevent multiple instances from being generated. This example allows you to create multiple instances of a todoEditor that loads and edits a todo instance:

You can see in this example that you can edit one todo and the other todos update. This is because each todoEditor is acting on same instance in memory. When it updates the todo's name here:

var updateData = function(newName) {
  todo.name = newName; // update name on todo instance
  ...
};

The other widgets update because they are bound to the same instance:

todo.on("name", updateElement); // when todo name changes update input element
todosConnection.addInstanceReference(todo); // previous line is a new usage of todo, so increase reference count

Each todoEditor receives the same instance because it was added to the connnection.instanceStore by addInstanceReference. During all instance retrievals, a connection using the constructor/store behavior checks the instanceStore for an instance with a matching id and return that if it exists. This example always requests id: 5, so all the todoEditors use the same instance held in the instanceStore.

This widget cleans itself up when it is removed by removing the listener on the todo instance and reducing the instance reference count:

todo.off("name", updateElement); // stop listening to todo name change
todosConnection.deleteInstanceReference(todo); // previous line removed a usage of todo, so reduce reference count

This is done to prevent a memory leak produced by keeping instances in the instanceStore when they are no longer needed by the application.

Note: a hazard of sharing the same instance is that if new instance data is loaded from the server during on-going editing of the instance, the new server data will replace the data that is edited but not yet saved. This is because whenever data is loaded from the server, it is passed to updatedInstance which updates the shared instance properties with the new server data.

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

On this page

Get help

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