can-connect/constructor/store/store
Supports saving and retrieving lists and instances in a store.
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);