DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
    • can-component
    • can-compute
    • can-connect
      • behaviors
        • ./base/
        • ./cache-requests/
        • ./can/map/
          • map static methods
            • get
            • getList
          • map instance methods
            • destroy
            • isDestroying
            • isNew
            • isSaving
            • save
          • hydrators
            • List
            • Map
            • instance
            • list
          • serializers
            • serializeInstance
            • serializeList
          • identifiers
            • id
          • instance callbacks
            • createdInstance
            • destroyedInstance
            • updatedInstance
            • updatedList
        • ./can/ref/
        • ./constructor/callbacks-once/
        • ./constructor/
        • ./constructor/store/
        • ./data/callbacks/
        • ./data/callbacks-cache/
        • ./data/combine-requests/
        • ./data/localstorage-cache/
        • ./data/memory-cache/
        • ./data/parse/
        • ./data/url/
        • ./data/worker/
        • ./fall-through-cache/
        • ./real-time/
      • modules
        • ./can/base-map/
        • ./can/model/
        • ./can/super-map/
        • ./can/tag/
        • ./helpers/weak-reference-map
      • data types
        • DataInterface
        • Instance
        • InstanceInterface
        • List
        • ListData
    • can-define
    • can-define/list/list
    • can-define/map/map
    • can-route
    • can-route-pushstate
    • can-set
    • can-stache
    • can-stache/helpers/route
    • can-stache-bindings
  • Ecosystem
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Core
  • /
  • can-connect
  • /
  • ./can/map/
  • / On this page
    • can-connect/can/map/map

      module

      Make a connection use a can-define/map/map type.

      • source

      canMap( baseConnection )

      Implements the hydrators, serializers, identifiers, and instance callback interfaces of can-connect/constructor/constructor so they work with a can-define/map/map and can-define/list/list. Adds static methods like getList and prototype methods like isDestroying to the Map type that make use of the connection's methods.

      Use

      The can-connect/can/map/map behavior make a connection use instances of a can-define/map/map and can-define/list/list. It also adds methods to the can-define/map/map that use the connection for retrieving, creating, updating, and destroying Map instances.

      To use can-connect/can/map/map, first create a Map and List constructor function:

      var Todo = DefineMap.extend({
        canComplete: 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 a connection that connects Todo and TodoList to a restful URL:

      var connect = require("can-connect");
      
      var todoConnection = connect([
          require("can-connect/data/url/url"),
          require("can-connect/constructor/constructor"),
          require("can-connect/can/map/map")
      ],{
        Map: Todo,
        List: TodoList,
        url: "/services/todos"
      });
      

      Now the connection can be used to CRUD Todo and TodoLists:

      todoConnection.getList({}).then(function(todos){
        var incomplete = todos.incomplete();
        incomplete[0].canComplete( 5 ) //-> true
      });
      

      However, because can/map adds methods to the Map option, you can use Todo directly to retrieve Todo and TodoLists:

      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:

      new Todo({name: "dishes"}).save().then(function(todo){
        todo.set({
            name: "Do the dishes"
          })
          .save()
          .then(function(todo){
            todo.destroy();
          });
      });
      

      There's also methods that let you know if an instance is in the process of being saved or destroyed:

      var savePromise = new Todo({name: "dishes"}).save();
      todo.isSaving() //-> true
      
      savePromise.then(function(){
          todo.isSaving() //-> false
      
          var destroyPromise = todo.destroy();
          todo.isDestroying() //-> true
      
          destroyPromise.then(function(){
      
              todo.isDestroying() //-> false
          })
      })
      

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