addListReference
Add a reference to the listStore so a list can be easily looked up.
connection.addListReference( list[, set] )
Adds a reference to a list by set
(or by listQuery) to the
listStore. Keeps a count of the number of references, removing the list
from the store when the count reaches 0.
Use
The listStore contains a mapping of lists keyed by their set
. The
listStore is used to prevent creating the same list multiple times and for
identifying a list for a given set. Lists need to be added to this store for this to work. To do this, call
addListReference
:
// A basic connection:
var constructorStore = require("can-connect/constructor/store/");
var constructor = require("can-connect/constructor/");
var dataUrl = require("can-connect/data/url/");
var todoConnection = connect([dataUrl, constructorStore, constructor], {
url: "/todos"
});
var dueToday;
// get a todo list
todoConnection.getList({due: "today"}).then(function( todos ){
// add it to the store
todoConnection.addListReference(todos, {due: "today"});
dueToday = todos;
});
Now, if you were to retrieve the same set of data sometime later, it would be the same list instance:
todoConnection.get({due: "today"}).then(function( todos ){
todos === dueToday //-> true
});
The .getListData
response data (underlying the call to todoConnection.getList
) is passed, along with the
existing list (dueToday
) to updatedList. That updates the shared list
instance with the newly retrieved data.
All the referenced lists stay in memory. Use deleteListReference to remove them.
Typically, addListReference
is called when something expresses interest in the list, such
as an event binding, and deleteListReference
is called when interest is removed.