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/
        • ./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/
          • options
            • ajax
            • url
          • data methods
            • createData
            • destroyData
            • getData
            • getListData
            • updateData
        • ./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
  • /
  • ./data/url/
  • / On this page
    • can-connect/data/url/url

      module
      • source

      connect.Behavior

      Uses the url option to implement the behavior of getListData, getData, createData, updateData, and destroyData to make an AJAX request to urls.

      Use

      The data/url behavior implements many of the DataInterface methods to send instance data to a URL.

      For example, the following todoConnection:

      var todoConnection = connect([
        require("can-connect/data/url/url")
      ],{
        url: {
          getListData: "GET /todos",
          getData: "GET /todos/{id}",
          createData: "POST /todos",
          updateData: "PUT /todos/{id}",
          destroyData: "DELETE /todos/{id}"
        }
      });
      

      Will make the following request when the following methods are called:

      // GET /todos?due=today
      todoConnection.getListData({due: "today"});
      
      // GET /todos/5
      todosConnection.getData({id: 5})
      
      // POST /todos \
      // name=take out trash
      todosConnection.createData({
        name: "take out trash"
      });
      
      // PUT /todos/5 \
      // name=do the dishes
      todosConnection.updateData({
        name: "do the dishes",
        id: 5
      });
      
      // DELETE /todos/5
      todosConnection.destroyData({
        id: 5
      });
      

      There's a few things to notice:

      1. URL values can include simple templates like {id} that replace that part of the URL with values in the data passed to the method.
      2. GET and DELETE request data is put in the URL using param.
      3. POST and PUT requests put data that is not templated in the URL in POST or PUT body as JSON-encoded data. To use form-encoded requests instead, add the property contentType:'application/x-www-form-urlencoded' to your url.
      4. If a provided URL doesn't include the method, the following default methods are provided:
        • getListData - GET
        • getData - GET
        • createData - POST
        • updateData - PUT
        • destroyData - DELETE

      If url is provided as a string like:

      var todoConnection = connect([
        require("can-connect/data/url/url")
      ],{
        url: "/todos"
      });
      

      This does the same thing as the first todoConnection example.

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