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/
        • ./constructor/store/
          • stores
            • addInstanceReference
            • addListReference
            • deleteInstanceReference
            • deleteListReference
            • instanceStore
            • listStore
          • 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/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/store/
  • /
  • addInstanceReference
  • / On this page
    • addInstanceReference

      function

      Adds a reference to an instance so it can be easily looked up.

      • source

      connection.addInstanceReference( instance )

      Adds a reference to an instance in the instanceStore by id. The number of references are incremented.

      Parameters

      1. instance {Instance}:

        The instance to add.

      Use

      The instanceStore contains a collection of instances created for each id. The instanceStore is used to prevent creating the same instance multiple times. Instances need to be added to this store for this behavior to happen. To do this, call addInstanceReference like the following:

      // A basic connection:
      var todoConnection = connect([
        require("can-connect/constructor/store/store"),
        require("can-connect/constructor/constructor"),
        require("can-connect/data/url/url")
      ], {
        url: "/todos"
      });
      
      var originalTodo;
      
      // Get a todo:
      todoConnection.get({id: 5}).then(function( todo ){
      
        // Add it to the store
        todoConnection.addInstanceReference(todo);
        originalTodo = todo;
      });
      

      Now, if you were to retrieve the same data sometime later, it would be the same instance.

      todoConnection.get({id: 5}).then(function( todo ){
      
        todo === originalTodo //-> true
      });
      

      The .getData response data is passed with originalTodo to updatedInstance which can update the originalTodo with the new data.

      All these instances stay in memory. Use deleteInstanceReference to remove them.

      Typically, addInstanceReference is called when something expresses interest in the interest, such as an event binding, and deleteInstanceReference is called when interest is removed.

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