create
Specifies how to create a new resource on the server. create(serialized)
is called by save if the model instance is new.
can.Model.create: function(serialized) -> deferred
Specify a function to create persistent instances. The function will typically perform an AJAX request to a service that results in creating a record in a database.
Parameters
-
serialized
{Object}
The serialized properties of the model to create.
Returns
can.Model.create: "[METHOD] /path/to/resource"
Specify a HTTP method and url to create persistent instances.
If you provide a URL, the Model will send a request to that URL using the method specified (or POST if none is specified) when saving a new instance on the server. (See below for more details.)
Parameters
-
METHOD
{HttpMethod}
An HTTP method. Defaults to
"POST"
. -
url
{STRING}
The URL of the service to retrieve JSON data.
can.Model.create: {ajaxSettings}
Specify an options object that is used to make a HTTP request to create persistent instances.
Parameters
-
ajaxSettings
{can.AjaxSettings}
A settings object that specifies the options available to pass to can.ajax.
create(attributes) -> Deferred
is used by save to create a model instance on the server.Implement with a URL
The easiest way to implement create is to give it the url to post data to:
This lets you create a recipe like:
Implement with a Function
You can also implement create by yourself. Create gets called with
attrs
, which are the serialized model attributes. Create returns aDeferred
that contains the id of the new instance and any other properties that should be set on the instance.For example, the following code makes a request to
POST /recipes.json {'name': 'hot+dog'}
and gets back something that looks like:The code looks like: