can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
73 lines (51 loc) • 1.84 kB
Markdown
data into an object that can be used to
create a [can.Model] instance.
`can.Model.parseModel( data, xhr )`
2.1
{Object} data The data to convert to a can.Model instance.
{XMLHTTPRequest} xhr The XMLHTTPRequest object used to make the request.
{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 [can.Model.findOne findOne],
[can.Model.update update], and [can.Model.create create] request
into an object that [can.Model.model] can use to create
a model instances.
This method is never called directly. Instead the deferred returned
by `findOne`, `update`, and `create` is piped into `parseModel`. If `findOne` was called,
the result of that is sent to [can.Model.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, [can.Model.model] expects data to be a name-value pair
object like:
```
{id: 1, name : "dishes"}
```
If your data does not look like this, you probably want to overwrite `parseModel`.
## Overwriting parseModel
If your service returns data like:
```
{ thingsToDo: {name: "dishes", id: 5} }
```
You will want to overwrite `parseModel` to pass the model what it expects like:
```
Task = can.Model.extend({
parseModel: function(data){
return data.thingsToDo;
}
},{});
```
You could also do this like:
```
Task = can.Model.extend({
parseModel: "thingsToDo"
},{});
```
can.Model.parseModel parseModel
can.Model.static
Convert raw