can-super-model
Connect a type to a restful data source, automatically manage lists, combine requests, and use a fall-through localstorage cache.
superModel(options)
superModel
an advanced data model that CanJS applications can
use. It requires a properly configured can-query-logic which experimenters might
find cumbersome to configure. If you are experimenting with CanJS, or have a
very irregular service layer, can-rest-model might be a better fit. If you don't need
or want caching, use can-realtime-rest-model. superModel
adds the following to can-realtime-rest-model:
- fall-through-cache - Saves all data in localStorage. Uses the data in localStorage to present data to the user immediately, but still makes the request and updates the user presented data and localStorage data in the background.
- combine-requests - Combines multiple request made at approximately the same time into a single request.
- can/ref - Reference types that make relationships easier.
If your service layer matches what superModel
expects, configuring
superModel
is very simple. For example,
the following defines a Todo
and TodoList
type and extends them
with the ability to connect to a restful service layer:
import {DefineMap, DefineList, superModel} from "can";
const Todo = DefineMap.extend("Todo",{
id: {identity: true},
name: "string",
complete: "boolean"
})
const TodoList = DefineList.extend("TodoList",{
get completeCount(){
return this.filter({complete: true}).length;
}
})
const todoConnection = superModel({
Map: Todo,
List: TodoList,
url: "/todos/{id}"
});
Parameters
- options
{Object}
:Configuration options supported by all the mixed-in behaviors:
Map - The map type constructor function used to create instances of the raw record data retrieved from the server. The type will also be decorated with the following methods:
List - The list type constructor function used to create a list of instances of the raw record data retrieved from the server.
url - Configure the URLs used to create, retrieve, update and delete data. It can be configured with a single url like:
url: "/services/todos/{_id}"
Or an object that configures how to create, retrieve, update and delete individually:
url: { getListData: "GET /services/todos", getData: "GET /services/todo/{id}", createData: "POST /services/todo", updateData: "PUT /services/todo/{id}", destroyData: "DELETE /services/todo/{id}" }
ajax - Specify a method to use to make requests. can-ajax is used by default. But jQuery's
.ajax
method can be passed.parseInstanceProp - Specify the property to find the data that represents an instance item.
parseInstanceData - Returns the properties that should be used to make an instance given the results of getData, createData, updateData, and destroyData.
parseListProp Specify the property to find the list data within a
getList
response.parseListData Return the correctly formatted data for a
getList
response.queryLogic - Specify the identity properties of the type. This is built automatically from the
Map
if can-define/map/map is used.
Returns
{connection}
:
A connection that is the combination of the options and all the behaviors
that superModel
adds.
superModel(url)
Create a connection with just a url. Use this if you do not need to pass in any other options
to configure the connection.
For example, the following creates a Todo
type with the ability to connect to a restful service layer:
import {todoFixture} from "//unpkg.com/can-demo-models@5";
import {superModel} from "can";
// Creates a mock backend with 5 todos
todoFixture(5);
const Todo = superModel("/api/todos/{id}").Map;
// Prints out all todo names
Todo.getList().then(todos => {
todos.forEach(todo => {
console.log(todo.name);
})
})
Parameters
- url
{String}
:The url used to create, retrieve, update and delete data.
Returns
{connection}
:
A connection that is the combination of the options and all the behaviors
that superModel
adds. The connection
includes a Map
property which is the type
constructor function used to create instances of the raw record data retrieved from the server.