list
Behavior option provided to create a typed list from a raw array.
connection.list( listData, set )
Takes a listData
argument with a data
property, that is an array of typed instances, each produced by
hydrateInstance, and returns a new typed list containing those typed
instances.
This method is passed as an option to the connection.
Called by hydrateList.
Parameters
Returns
{List}
:
a typed list type containing the typed instances
Usage
The following example makes the connection produce MyList
typed lists including a completed
method:
var constructor = require("can-connect/constructor/");
var dataUrl = require("can-connect/data/url/");
// define custom list type constructor
var MyList = function(items) {
this.push.apply(this, items);
}
// inherit Array functionality
MyList.prototype = Object.create(Array.prototype);
// add custom methods to new list type
MyList.prototype.completed = function(){
return this.filter(function(){ return this.completed });
};
// create connection
var todosConnection = connect([constructor, dataUrl], {
url: "/todos",
list: function(listData, set){
// create custom list instance
var collection = new MyList(listData.data);
// add set info for use by other behaviors
collection.__listSet = set;
return collection;
}
});
// use connection to get typed list & use custom method
todosConnection.getList({}).then(function(todoList){
console.log("There are", todoList.completed().length, "completed todos");
});
Note: we added the listSetProp property (__listSet
by default) on the list. This is
expected by other behaviors.