model

  • function
can.Model.model  

Convert raw data into a can.Model instance.

can.Model.model(data)

Parameters

  1. data {Object}

    The data to convert to a can.Model instance.

Returns

{can.Model}

An instance of can.Model made with the given data.

model: "PROPERTY"

Creates a model function that looks for the attributes object in the PROPERTY property of raw instance data.

can.Model.model(attributes) is used to convert data from the server into a model instance. It is rarely called directly. Instead it is invoked as a result of findOne or findAll.

If your server is returning data in non-standard way, overwriting can.Model.model is a good way to normalize it.

Example

The following uses model to convert to a model instance.

Task = can.Model.extend({},{})
var task = Task.model({id: 1, name : "dishes", complete : false})

tasks.attr("complete", true)

Task.model(attrs) is very similar to simply calling new Model(attrs) except that it checks the model's store if the instance has already been created. The model's store is a collection of instances that have event handlers.

This means that if the model's store already has an instance, you'll get the same instance back. Example:

// create a task
var taskA = new Task({id: 5, complete: true});

// bind to it, which puts it in the store
 taskA.bind("complete", function(){});

// use model to create / retrieve a task
var taskB = Task.model({id: 5, complete: true});

taskA === taskB //-> true

Non-standard Services

can.Model.model expects to retreive attributes of the model instance like:

{id: 5, name : "dishes"}

If the service returns data formatted differently, like:

{todo: {name: "dishes", id: 5}}

Overwrite model like:

Task = can.Model.extend({
  model : function(data){
    return can.Model.model.call(this,data.todo);
  }
},{});