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/
            • behavior options
              • instance
              • list
            • CRUD methods
              • destroy
              • get
              • getList
              • save
            • CRUD callbacks
              • createdInstance
              • destroyedInstance
              • updatedInstance
              • updatedList
            • hydrators
              • hydrateInstance
              • hydrateList
            • serializers
              • serializeInstance
              • serializeList
            • helpers
              • cidStore
              • isNew
          • ./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

  • Edit on GitHub

Adds an interface to interact with custom types via the connection instead of plain Objects and Arrays.

constructor( baseConnection )

Adds an interface that allows the connection to operate on custom types. These fall into the categories:

  • CRUD Methods - create, read, update and delete typed instances via the data source
  • CRUD Callbacks - activities run on typed instances following data source operations
  • Hydrators - conversion of raw data to typed data
  • Serializers - conversion of typed data to raw data

Parameters

  1. baseConnection {Object}:

    can-connect connection object that is having the constructor behavior added on to it.

Returns

{Object}:

A can-connect connection containing the method implementations provided by constructor.

Use

The constructor behavior allows you to instantiate the raw representation of the data source's data into a custom typed representation with additional methods and behaviors. An example might be loading data from a "/todos" service and being able to call .timeLeft() on the todos that you get back like:

todoConnection.get({id: 6}).then(function(todo){
  todo.timeLeft() //-> 60000
})

The following creates a todoConnection that does exactly that:

// require connection plugins
var constructor = require("can-connect/constructor/");
var dataUrl = require("can-connect/data/url/");

// define type constructor function
var Todo = function(data){
  // add passed properties to new instance
  for(var prop in data) {
   this[prop] = data;
  }
};

// add method to get time left before due, in milliseconds
Todo.prototype.timeLeft = function(){
  return new Date() - this.dueDate
};

// create connection, passing function to instantiate new instances
var todoConnection = connect([constuctor, dataUrl], {
  url: "/todos",
  instance: function(data){
    return new Todo(data);
  }
});

The constructor behavior is still useful even if you want to keep your data as untyped objects (which is the default behavior when no instance implementation is provided). The behavior provides an interface to the data held by the client. For example, updatedInstance provides an extension point for logic that needs to be executed after an instance held by the client finishes an update request. This is valuable whether that instance is typed or not. Extensions like real-time or fall-through-cache require this interface for advanced behavior.

Interface

constructor provides the following categories of methods to interact with typed data:

CRUD Methods

Methods that create, read, update and delete (CRUD) typed representations of raw connection data:

  • get - retrieve a single typed instance from the data source
  • getList - retrieve a typed list of instances from the data source
  • save - save a typed instance's data to the data source
  • destroy - delete a typed instance's data from the data source

CRUD Callbacks

"CRUD Methods" call these methods with request response data and a related instance. Their implementation here updates the related instance with that data:

  • createdInstance - after saving new instance to data source, update that instance with response data
  • updatedInstance - after saving existing instance to data source, update that instance with response data
  • destroyedInstance - after deleting instance from data source, update that instance with response data
  • updatedList - after new data is read from the data source, update an existing list with instances created from that data

Hydrators

These methods are used to create a typed instance or typed list given raw data objects:

  • hydrateInstance - create a typed instance given raw instance data
  • hydrateList - create a typed list of typed instances given given raw list data

Serializers

These methods convert a typed instance or typed list into a raw object:

  • serializeInstance - return raw data representing the state of the typed instance argument
  • serializeList - return raw data representing the state of the typed list argument

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