can-connect/can/super-map/super-map
Create connection with many of the best behaviors in can-connect and hook it up to a can-define/map/map.
superMap(options)
Creates a connection with the following behaviors: constructor, can/map, constructor/store, data/callbacks, data/callbacks-cache, combine-requests, can-connect/data/parse/parse, data/url, real-time, fall-through-cache, constructor/callbacks-once.
And creates a localstorage-cache to use as a cacheConnection.
Use
The can-connect/can/super-map
module exports a helper function that creates a connection
with the "advanced" behaviors in can-connect and hooks it up to a can-define/map/map
and can-define/list/list.
If you are using CanJS, this is an easy way to create a connection that can be useful and fast in most circumstances.
To use it, first define a Map and List constructor function:
var DefineMap = require("can-define/map/map");
var DefineList = require("can-define/list/list");
var Todo = DefineMap.extend({ ... });
var TodoList = DefineList.extend({
"#": Todo
});
Next, call superMap
with all of the options needed by the behaviors that superMap
adds:
var todoConnection = superMap({
idProp: "_id",
Map: Todo,
List: TodoList,
url: "/services/todos",
name: "todo"
});
can/map adds CRUD methods to the Map
option, you can use those to create,
read, update and destroy todos:
Todo.getList({}).then(function(todos){ ... });
Todo.get({}).then(function(todo){ ... });
new Todo({name: "dishes"}).save().then(function(todo){
todo.set({
name: "Do the dishes"
})
.save()
.then(function(todo){
todo.destroy();
});
});