DoneJS StealJS jQuery++ FuncUnit DocumentJS
5.33.3
6.0.0 4.3.0 3.14.1 2.3.35
  • About
  • Guides
  • API Docs
  • Community
  • Contributing
  • Bitovi
    • Bitovi.com
    • Blog
    • Design
    • Development
    • Training
    • Open Source
    • About
    • Contact Us
  • About
  • Guides
  • API Docs
    • Observables
      • can-bind
      • can-compute
      • can-debug
      • can-define
      • can-define/list/list
      • can-define/map/map
      • can-define-backup
      • can-define-stream
      • can-define-stream-kefir
      • can-event-queue
      • can-kefir
      • can-list
      • can-map
      • can-map-compat
      • can-map-define
      • can-observable-array
      • can-observable-object
      • can-observation
      • can-observation-recorder
      • can-observe
      • can-simple-map
      • can-simple-observable
      • can-stream
      • can-stream-kefir
      • can-value
    • Views
      • can-attribute-observable
      • can-component
      • can-stache
      • can-stache-bindings
      • can-stache-converters
      • can-stache-element
      • can-stache-route-helpers
      • can-view-autorender
      • can-view-callbacks
      • can-view-import
      • can-view-live
      • can-view-model
      • can-view-nodelist
      • can-view-parser
      • can-view-scope
      • can-view-target
      • steal-stache
    • Data Modeling
      • can-connect
      • can-connect-feathers
      • can-connect-ndjson
      • can-connect-tag
      • can-fixture
      • can-fixture-socket
      • can-local-store
      • can-memory-store
      • can-ndjson-stream
      • can-query-logic
      • can-realtime-rest-model
      • can-rest-model
      • can-set-legacy
      • can-super-model
    • Routing
      • can-deparam
      • can-param
      • can-route
      • can-route-hash
      • can-route-mock
      • can-route-pushstate
    • JS Utilities
      • can-assign
      • can-define-lazy-value
      • can-diff
      • can-globals
      • can-join-uris
      • can-key
      • can-key-tree
      • can-make-map
      • can-parse-uri
      • can-queues
      • can-string
      • can-string-to-any
      • can-zone-storage
    • DOM Utilities
      • can-ajax
      • can-attribute-encoder
      • can-child-nodes
      • can-control
      • can-dom-data
      • can-dom-events
      • can-dom-mutate
      • can-event-dom-enter
      • can-event-dom-radiochange
      • can-fragment
    • Data Validation
      • can-define-validate-validatejs
      • can-type
      • can-validate
      • can-validate-interface
      • can-validate-legacy
      • can-validate-validatejs
    • Typed Data
      • can-cid
      • can-construct
      • can-construct-super
      • can-data-types
      • can-namespace
      • can-reflect
      • can-reflect-dependencies
      • can-reflect-promise
      • can-types
    • Polyfills
      • can-symbol
      • can-vdom
    • Core
    • Infrastructure
      • can-global
      • can-test-helpers
    • Ecosystem
    • Legacy
  • Community
  • Contributing
  • GitHub
  • Twitter
  • Chat
  • Forum
  • News
Bitovi

can-super-model

  • npm package badge
  • Star
  • Edit on GitHub

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

  1. 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:

      • getList
      • get
      • save
      • destroy
      • isSaving
      • isDestroying
      • isNew
    • 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

  1. 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.

CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 5.33.3.

On this page

Get help

  • Chat with us
  • File an issue
  • Ask questions
  • Read latest news