can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
152 lines (110 loc) • 4.12 kB
Markdown
when an error occurs. The callback receives the
XmlHttpRequest object.
{can.Deferred} A deferred that resolves to a [can.Model.List] of retrieved models.
`can.Model.findAll: findAllData( params ) -> deferred`
Implements `findAll` with a [can.Model.findAllData function]. This function
is passed to [can.Model.makeFindAll makeFindAll] to create the external
`findAll` method.
```
findAll: function(params){
return $.get("/tasks",params)
}
```
{can.Model.findAllData} findAllData 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 [can.Model.findAllData findAllData function]
which is passed to [can.Model.makeFindAll makeFindAll] to create the external
`findAll` method.
{HttpMethod} METHOD An HTTP method. Defaults to `"GET"`.
{STRING} url The URL of the service to retrieve JSON data.
{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 [can.Model.models] to turn it into instances.
_Note: .findAll can also accept an array, but you
probably [should not be doing that](http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx)._
`can.Model.findAll: {ajaxSettings}`
Implements `findAll` with a [can.AjaxSettings ajax settings object].
```
findAll: {url: "/tasks", dataType: "json"}
```
If `findAll` is implemented with an object, it gets converted to
a [can.Model.findAllData findAllData function]
which is passed to [can.Model.makeFindAll makeFindAll] to create the external
`findAll` method.
{can.AjaxSettings} 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 implementing `findAll`, use it to retrieve instances of the model
like:
```
Recipe.findAll({favorite: true}, function(recipes){
recipes[0].attr('name') //-> "Ice Water"
}, function( xhr ){
// called if an error
}) //-> Deferred
```
`findAll` uses [can.Model.parseModels parseModels] to parse the returned data. `can.Model.parseModels`
can be overwritten to handle non-standard data formats.
Before you can use `findAll`, you must implement it.
## Implement with a URL
Implement findAll with a url like:
```
Recipe = can.Model.extend({
findAll : "/recipes.json"
},{});
```
The server should return data that looks like:
```
[
{"id" : 57, "name": "Ice Water"},
{"id" : 58, "name": "Toast"}
]
```
## Implement with an Object
Implement findAll with an object that specifies the parameters to
`can.ajax` (jQuery.ajax) like:
```
Recipe = can.Model.extend({
findAll : {
url: "/recipes.xml",
dataType: "xml"
}
},{});
```
## 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:
```
Recipe = can.Model.extend({
findAll : function(params){
return $.ajax({
url: '/recipes.json',
type: 'get',
dataType: 'json'})
}
},{});
```
Retrieve multiple resources from a server.
can.Model.findAll findAll
can.Model.static
`can.Model.findAll( params[, success[, error]] )`
Retrieve multiple resources from a server.
{Object} params Values to filter the request or results with.
{function(can.Model.List)} [success(list)] A callback to call on successful retrieval. The callback receives
a can.Model.List of the retrieved resources.
{function(can.AjaxSettings)} [error(xhr)] A callback to call