DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
      • behaviors
        • ./base/
        • ./cache-requests/
        • ./can/map/
        • ./can/ref/
        • ./constructor/callbacks-once/
        • ./constructor/
          • crud methods
            • destroy
            • get
            • getList
            • save
          • crud callbacks
            • createdInstance
            • destroyedInstance
            • updatedInstance
            • updatedList
          • hydrators
            • hydrateInstance
            • instance
            • list
            • hydrateList
          • serializers
            • serializeInstance
            • serializeList
          • helpers
            • cidStore
            • isNew
        • ./constructor/store/
        • ./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/weak-reference-map
      • data types
        • DataInterface
        • Instance
        • InstanceInterface
        • List
        • ListData
    • can-define
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-connect
  • /
  • ./constructor/
  • / On this page
    • can-connect/constructor/constructor

      module

      Adds the ability to operate on special types instead of plain JavaScript Objects and Arrays.

      • source

      constructor( baseConnection )

      Adds methods that allow the connection to operate on special types.

      Parameters

      1. baseConnection {connection}:

        A connection with most of the DataInterface implemented.

      Returns

      {connection}:

      A new connection with the additional methods.

      Use

      The can-connect/constructor/constructor behavior allows you to hydrate the raw, serialized representation of your application's data into a typed representation with additional methods and behaviors.

      For example, you might want to be able to load data as a particular JavaScript Constructor function that has a helper methods that act upon the serialized data.

      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:

      var Todo = function(data){
        for(var prop in data) {
         this[prop] = data;
        }
      };
      Todo.prototype.timeLeft = function(){
        return new Date() - this.dueDate
      };
      
      var todoConnection = connect([
        require("can-connect/constructor/constructor"),
        require("can-connect/data/url/url")
      ],{
        url: "/todos"
        instance: function(data){
          return new Todo(data);
        }
      });
      

      The constructor extension is still useful even if you want to keep your data as plain JavaScript objects (which its default behavior). The constructor extension describes the in-memory representation of your data on the client. Other extensions need to know this representation for advanced behavior like can-connect/real-time/real-time or can-connect/fall-through-cache/fall-through-cache.

      CRUD Methods

      The constructor extension supplies methods that create, read, update and delete (CRUD) typed representations of raw connection data.

      CRUD Callbacks

      The constructor function "CRUD Methods" call "CRUD Callbacks" with the the "data interface" response data. These callbacks update the state of the typed representation.

      Instantaitors

      These methods are used to create a typed instance or typed list given raw "data interface" objects.

      Serializers

      These methods convert the typed instance or typed list into a representation for the "data interface".

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