can.Model.List
inherits: can.List
A list connected to can.Model's CRUD abilities.
new can.Model.List()
Create an empty model list.
new can.Model.List( [models] )
new can.Model.List( deferred )
Create a model list with the results of deferred
.
Parameters
-
deferred
{Deferred.Array<can.Model | Object>}
A promise that will resolve to an array. Once the promise resolves, the
List
will have its contents replaced as ifnew can.Model.List(array)
had been called.
new can.Model.List( params )
Create an initially empty model list, but use the model's findAll to get a list of models and add it to this empty list.
Parameters
-
params
{Object}
Params that are passed to the Map property's findAll method.
Use
can.Model.List
is a can.List associated with a can.Model.can.Model.List
s are just like can.List except they have a few super-powers:findAll
).Defining a model list
When can.Model is extended,
can.Model.List
is automatically extended and set as that model's List property. Typically, acan.Model.List
is defined for you. For example:This List type is returned by findAll:
The List's Map property points to the extended can.Model:
Defining custom
can.Model.Lists
allows you to extend lists with helper functions for a list of a specific type. The following adds the ability to retrieve the number of completed and remaining todos:Creating a model list instance
If you use findAll, it calls back with and resolves to a model list:
To create an empty model list yourself, use
new {model_name}.List()
like:You can also pass an array to instantiate the list with the array data using
new {model_name}.List(ARRAY)
. It can be either an array of models:...or an array of objects. If objects are provided, model list will convert them to models. The following does the same thing as the previous example:
If, instead of using an array, a model list is created with a plain JavaScript object like:
The object is assumed to be parameters to the list's Model's findAll method. An empty list will be returned, but
Todo.findAll
will be called. The items it returns will be inserted into the list.Removing models from model list
One advantage that
can.Model.List
has over a traditionalcan.List
is that when you destroy a model, if it is in that list, it will automatically be removed from the list.