parseModel
can.Model.parseModel
Convert raw data into an object that can be used to create a can.Model instance.
can.Model.parseModel( data, xhr ) 2.1
Parameters
-
data
{Object}
The data to convert to a can.Model instance.
-
xhr
{XMLHTTPRequest}
The XMLHTTPRequest object used to make the request.
Returns
{Object}
An object of properties to set at the [can.Model::attr attributes] of a model instance.
parseModel: "PROPERTY"
Creates a parseModel
function that looks for the attributes object in the PROPERTY
property of raw instance data.
Use
can.Model.parseModel(data, xhr)
is used to convert the raw response of a findOne, update, and create request into an object that model can use to create a model instances.This method is never called directly. Instead the deferred returned by
findOne
,update
, andcreate
is piped intoparseModel
. IffindOne
was called, the result of that is sent to model.If your server is returning data in non-standard way, overwriting
can.Model.parseModel
is the best way to normalize it.Expected data format
By default, model expects data to be a name-value pair object like:
If your data does not look like this, you probably want to overwrite
parseModel
.Overwriting parseModel
If your service returns data like:
You will want to overwrite
parseModel
to pass the model what it expects like:You could also do this like: