can/map
Integrate a can-connect
connection with a DefineMap type.
canMap( baseConnection )
Extends the functionality of the constructor behavior so it integrates tightly with DefineMap and DefineList types:
- adds methods to read, destroy, create and update instances (via the connection) to the Map type
- adds observable values to instances indicating if they are being saved, deleted or have not yet been saved
- updates instances with the data from the response bodies of create, update and delete requests
- triggers events on the Map type and instances when instances are created, destroyed or updated
Parameters
- baseConnection
{Object}
:can-connect
connection object that is having thecan/map
behavior added on to it. Expects the constructor behavior to already be added to this base connection. If theconnect
helper is used to build the connection, the behaviors will automatically be ordered as required.
Returns
{Object}
:
a can-connect
connection containing the methods provided by can/map
.
Use
The can/map
behavior links a connection to a DefineMap and DefineList type.
The connection will create those types of instances from the data it receives. It also adds convenient methods and
observable values to the Map that offer connection functionality (e.g
instance.save
) and the status of the instance (e.g
instance.isSaving
).
To use the can/map
behavior, first create a Map and List constructor function:
var Todo = DefineMap.extend({
allowComplete: function(ownerId) {
return this.ownerId === ownerId;
}
});
var TodoList = DefineList.extend({
"#": Todo,
incomplete: function(){
return this.filter({complete: false});
}
});
Next, pass the Map and List constructor functions to connect
as options. The following creates connects the Todo
and TodoList
types to a RESTful data service via the connection:
import connect from "can-connect";
import dataUrl from "can-connect/data/url/url";
import constructor from "can-connect/constructor/constructor";
import canMap from "can-connect/can/map/map";
const todoConnection = connect( [ dataUrl, constructor, canMap ], {
Map: Todo,
List: TodoList,
url: "/services/todos"
} );
The connection itself can be used to create, read, update & delete Todo
and TodoList
s:
todoConnection.getList( {} ).then( function( todos ) {
const incomplete = todos.incomplete();
incomplete[ 0 ].allowComplete( 5 ); //-> true
} );
... or instead of how it's done above, because can/map
adds methods to the Map type, you
can use Todo
to retrieve Todo
and TodoList
s:
Todo.getList( {} ).then( function( todos ) { /* ... */ } );
Todo.get( {} ).then( function( todo ) { /* ... */ } );
You can also create, update, and destroy Todo
instances. Notice that
save is used to create and update:
// create an instance
new Todo( { name: "dishes" } ).save().then( function( todo ) {
todo.set( {
name: "Do the dishes"
} )
.save() // update an instance
.then( function( todo ) {
todo.destroy(); // destroy an instance
} );
} );
There's also methods that let you know if an instance is in the process of being saved or destroyed:
const savePromise = new Todo( { name: "dishes" } ).save();
todo.isSaving(); //-> true
savePromise.then( function() {
todo.isSaving(); //-> false
const destroyPromise = todo.destroy();
todo.isDestroying(); //-> true
destroyPromise.then( function() {
todo.isDestroying(); //-> false
} );
} );