findAll
Retrieve multiple resources from a server.
can.Model.findAll( params[, success[, error]] )
Retrieve multiple resources from a server.
Parameters
-
params
{Object}
Values to filter the request or results with.
-
success
{function(list)}
OptionalA callback to call on successful retrieval. The callback recieves a can.Model.List of the retrieved resources.
-
error
{function(xhr)}
OptionalA callback to call when an error occurs. The callback receives the XmlHttpRequest object.
Returns
{can.Deferred}
A deferred that resolves to a can.Model.List of retrieved models.
can.Model.findAll: findAllData( params ) -> deferred
Implements findAll
with a function. This function
is passed to makeFindAll to create the external
findAll
method.
findAll: function(params){
return $.get("/tasks",params)
}
Parameters
-
findAllData
{findAllData(params)}
A function that accepts parameters specifying a list of instance data to retrieve and returns a can.Deferred that resolves to an array of those instances.
can.Model.findAll: "[METHOD] /path/to/resource"
Implements findAll
with a HTTP method and url to retrieve instance data.
findAll: "GET /tasks"
If findAll
is implemented with a string, this gets converted to
a findAllData function
which is passed to makeFindAll to create the external
findAll
method.
Parameters
-
METHOD
{HttpMethod}
An HTTP method. Defaults to
"GET"
. -
url
{STRING}
The URL of the service to retrieve JSON data.
Returns
{JSON}
The service should return a JSON object like:
{
"data": [
{ "id" : 1, "name" : "do the dishes" },
{ "id" : 2, "name" : "mow the lawn" },
{ "id" : 3, "name" : "iron my shirts" }
]
}
This object is passed to models to turn it into instances.
Note: .findAll can also accept an array, but you probably should not be doing that.
can.Model.findAll: {ajaxSettings}
Implements findAll
with a ajax settings object.
findAll: {url: "/tasks", dataType: "json"}
If findAll
is implemented with an object, it gets converted to
a findAllData function
which is passed to makeFindAll to create the external
findAll
method.
Parameters
-
ajaxSettings
{can.AjaxSettings}
A settings object that specifies the options available to pass to can.ajax.
Use
findAll( params, success(instances), error(xhr) ) -> Deferred
is used to retrieve model instances from the server. After implementingfindAll
, use it to retrieve instances of the model like:Before you can use
findAll
, you must implement it.Implement with a URL
Implement findAll with a url like:
The server should return data that looks like:
Implement with an Object
Implement findAll with an object that specifies the parameters to
can.ajax
(jQuery.ajax) like:Implement with a Function
To implement with a function,
findAll
is passed params to filter the instances retrieved from the server and it should return a deferred that resolves to an array of model data. For example: