parseModels
2.1
can.Model.parseModels
Convert raw xhr data into an array or object that can be used to create a can.Model.List.
can.Model.parseModels(data, xhr)
parseModels: "PROPERTY"
Creates a parseModels function that looks for the array of instance data in the PROPERTY
property of the raw response data of findAll.
Use
can.Model.parseModels(data, xhr)is used to convert the raw response of a findAll request into an object or Array that models can use to create a can.Model.List of model instances.This method is never called directly. Instead the deferred returned by findAll is piped into
parseModelsand the result of that is sent to models.If your server is returning data in non-standard way, overwriting
can.Model.parseModelsis the best way to normalize it.Expected data format
By default, models expects data to be an array of name-value pair objects like:
It can also take an object with additional data about the array like:
In this case, models will return a can.Model.List of instances found in data, but with additional properties as expandos on the list:
If your data does not look like one of these formats, overwrite
parseModels.Overwriting parseModels
If your service returns data like:
You will want to overwrite
parseModelsto pass the models what it expects like:You could also do this like:
can.Model.modelspasses each instance's data tocan.Model.modelto create the individual instances.