findOne
Retrieve a resource from a server.
can.Model.findOne( params[, success[, error]] )
Retrieve a single instance from the server.
Parameters
-
params
{Object}
Values to filter the request or results with.
-
success
{function(model)}
OptionalA callback to call on successful retrieval. The callback receives the retrieved resource as a can.Model.
-
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 instance of the retrieved model
can.Model.findOne: findOneData( params ) -> deferred
Implements findOne
with a function. This function
is passed to makeFindOne to create the external
findOne
method.
findOne: function(params){
return $.get("/task/"+params.id)
}
Parameters
-
findOneData
{findOneData(params)}
A function that accepts parameters specifying an instance to retreive and returns a can.Deferred that resolves to that instance.
can.Model.findOne: "[METHOD] /path/to/resource"
Implements findOne
with a HTTP method and url to retrieve an instance's data.
findOne: "GET /tasks/{id}"
If findOne
is implemented with a string, this gets converted to
a makeFindOne function
which is passed to makeFindOne to create the external
findOne
method.
Parameters
-
METHOD
{HttpMethod}
An HTTP method. Defaults to
"GET"
. -
url
{STRING}
The URL of the service to retrieve JSON data.
can.Model.findOne: {ajaxSettings}
Implements findOne
with a [can.AjaxSettings ajax settings object].
findOne: {url: "/tasks/{id}", dataType: "json"}
If findOne
is implemented with an object, it gets converted to
a makeFindOne function
which is passed to makeFindOne to create the external
findOne
method.
Parameters
-
ajaxSettings
{can.AjaxSettings}
A settings object that specifies the options available to pass to can.ajax.
Use
findOne( params, success(instance), error(xhr) ) -> Deferred
is used to retrieve a model instance from the server.Use
findOne
like:Before you can use
findOne
, you must implement it.Implement with a URL
Implement findAll with a url like:
If
findOne
is called like:The server should return data that looks like:
Implement with an Object
Implement
findOne
with an object that specifies the parameters tocan.ajax
(jQuery.ajax) like:Implement with a Function
To implement with a function,
findOne
is passed params to specify the instance retrieved from the server and it should return a deferred that resolves to the model data. Also notice that you now need to build the URL manually. For example: