can
Version:
MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.
45 lines (31 loc) • 1.41 kB
Markdown
Model.models models
can.Model.static
{2.1} Prior to 2.1, `.models` was used to convert the ajax
responses into a data format useful for converting them into an observable
list AND for converting them into that list. In 2.1, [can.Model.parseModels] should
be used to convert the ajax responses into a data format useful to [can.Model.models].
Convert raw data into can.Model instances. Merge data with items in
the store if matches are found.
`can.Model.models(data[, oldList])`
{Array<Object>} data The raw data from a `[can.Model.findAll findAll()]` request.
{can.Model.List} [oldList] If supplied, this List will be updated with the data from
__data__.
{can.Model.List} A List of Models made from the raw data.
## Use
`.models(data)` is used to create a [can.Model.List] of [can.Model] instances
with the data provided. If an item in data matches an instance in the [can.Model.store],
that instance will be merged with the item's data and inserted in the list.
For example
```
Task = can.Model.extend({},{})
var t1 = new Task({id: 1, name: "dishes"});
// Binding on a model puts it in the store.
t1.bind("change", function(){})
var tasks = Task.models([
{id: 1, name : "dishes", complete : false},
{id: 2, name: "laundry", complete: true}
])
t1 === tasks.attr(0) //-> true
t1.attr("complete") //-> false
```
can.