can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
92 lines (64 loc) • 2.58 kB
Markdown
if the model instance [can.Model.prototype.isNew is new].
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.
the model to create.
that will be added to the created model instance. The object __MUST__ contain
an [can.Model.id id] property so that future calls to [can.Model.prototype.save save]
will call [can.Model.update].
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.)
Specify an options object that is used to make a HTTP request to create
persistent instances.
specifies the options available to pass to [can.ajax].
`create(attributes) -> Deferred` is used by [can.Model::save 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:
```
var Recipe = can.Model.extend({
create: "/recipes"
},{})
```
This lets you create a recipe like:
```
new Recipe({name: "hot dog"}).save();
```
## Implement with a Function
You can also implement create by yourself. Create gets called
with `attrs`, which are the [can.Map::serialize serialized] model
attributes. Create returns a `Deferred`
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:
```
{
"id": 5,
"createdAt": 2234234329
}
```
The code looks like:
```
can.Model.extend("Recipe", {
create : function( attrs ){
return $.post("/recipes.json",attrs, undefined ,"json");
}
},{})
```
by [can.Model.prototype.save save]