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/
  • / On this page
    • can-connect/constructor/store/store

      module

      Supports saving and retrieving lists and instances in a store.

      • source

      constructorStore( baseConnection )

      Overwrites baseConnection so it contains a store for instances and lists. It traps calls to the hydrateInstance and hydrateList methods to use instances or lists in the store if available. It overwrites "CRUD METHODS" to make sure that while any request is pending, all lists and instances are added to the store. Finally, it provides methods to add and remove items in the store via reference counting.

      Use

      The constructor-store extension is used to:

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

      The stores provide access to an instance by its id or a list by its listSet. These stores are used by other extensions like can-connect/real-time/real-time and can-connect/fall-through-cache/fall-through-cache.

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

      You'll notice that you can edit one todo's name and the other todo editors update. This is because each todoEditor gets the same instance in memory. So that when it updates the todo's name ...

      element.firstChild.onchange = function(){
        todo.name = this.value;
      };
      

      ... the other widgets update because they have bound on the same instance:

      Object.observe(todo, update, ["update"] );
      todosConnection.addInstanceReference(todo);
      

      Each todoEditor gets the same instance because they called addListReference which makes it so anytime a todo with id=5 is requested, the same instance is returned.

      Notice that if you change an input element, and click "Create Todo Editor", all the todoEditor widgets are set back to the old text. This is because whenever data is loaded from the server, it is passed to updatedInstance which defaults to overwriting any current properties with those from the server.

      To make sure the server has the latest, you can save a todo by hitting "ENTER".

      Finally, this widget cleans itself up nicely when it is removed by unobserving the todo instance and deleting the instance reference. Doing this prevents memory leaks.

      Object.unobserve(todo, update, ["update"] );
      todosConnection.deleteInstanceReference(todo);
      

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