addInstanceReference
Adds a reference to an instance so it can be easily looked up.
connection.addInstanceReference( instance )
Adds a reference to an instance in the instanceStore by id. The number of references are incremented.
Parameters
- 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.