create

  • function
can.Model.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

  1. serialized {Object}

    The serialized properties of the model to create.

Returns

{can.Deferred}

A Deferred that resolves to an object of attributes that will be added to the created model instance. The object MUST contain an id property so that future calls to save will call update.

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

  1. METHOD {HttpMethod}

    An HTTP method. Defaults to "POST".

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

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

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 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");
 }
},{})