update
can.Model.update
Update a resource on the server.
can.Model.update: "[METHOD] /path/to/resource"
If you provide a URL, the Model will send a request to that URL using the method specified (or PUT if none is specified) when updating an instance on the server. (See below for more details.)
Returns
{can.Deferred}
A Deferred that resolves to the updated model.
can.Model.update: function(id, serialized) -> can.Deffered
If you provide a function, the Model will expect you to do your own AJAX requests.
Parameters
-
id
The ID of the model to update.
-
serialized
{Object}
The serialized properties of the model to update.
Returns
{can.Deferred}
A Deferred that resolves to the updated model.
update( id, attrs ) -> Deferred
is used by save to update a model instance on the server.Implement with a URL
The easist way to implement update is to just give it the url to
PUT
data to:This lets you update a recipe like:
This will make an XHR request like:
If your server doesn't use PUT, you can change it to post like:
The server should send back an object with any new attributes the model should have. For example if your server updates the "updatedAt" property, it should send back something like:
Implement with a Function
You can also implement update by yourself. Update takes the
id
andattributes
of the instance to be updated. Update must return a Deferred that resolves to an object that contains any properties that should be set on the instance.For example, the following code makes a request to '/recipes/5.json?name=hot+dog' and gets back something that looks like:
The code looks like: