findAll

  • function
can.Model.findAll  

Retrieve multiple resources from a server.

can.Model.findAll( params[, success[, error]] )

Retrieve multiple resources from a server.

Parameters

  1. params {Object}

    Values to filter the request or results with.

  2. success {function(list)}Optional

    A callback to call on successful retrieval. The callback receives a can.Model.List of the retrieved resources.

  3. error {function(xhr)}Optional

    A 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

  1. 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

  1. METHOD {HttpMethod}

    An HTTP method. Defaults to "GET".

  2. 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 [can.AjaxSettings 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

  1. 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 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 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'})
 }
},{});