instance
Behavior option provided to create a typed form of passed raw data.
connection.instance( props )
Creates a typed instance for the passed raw data object. This method is passed as an option to the connection. Called by hydrateInstance.
Parameters
- props
{Object}
:a raw object containing the properties from the data source
Returns
{Instance}
:
the typed instance created from the passed props
object
Usage
The following example makes the connection produce Todo
typed objects including a complete
method:
var constructor = require("can-connect/constructor/");
var dataUrl = require("can-connect/data/url/");
// define type constructor
var Todo = function(rawData){
// add raw data to new instance
Object.assign(this, rawData);
};
// add methods to custom type
Todo.prototype.complete = function(){
this.completed = true;
}
// create connection
var todosConnection = connect([constructor, dataUrl], {
url: "/todos",
instance: function(rawData) {
return new Todo(rawData);
}
});
// use connection to get typed instance & use custom method
todosConnection.get({id: 5}).then(function(todo){
todo.completed; // false
todo.complete();
todo.completed; // true
});