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/
          • parseInstanceData
          • parseListData
          • parseListProp
          • parseInstanceProp
        • ./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
  • /
  • ./data/parse/
  • /
  • parseListData
  • / On this page
    • parseListData

      function

      Given a response from getListData returns its data in the proper ListData format.

      • source

      connection.parseListData(responseData)

      This function uses parseListProp to find the array containing the data for each instance. Then it uses parseInstanceData on each item in the array Finally, it returns data in the ListData format.

      Parameters

      1. responseData {Object}:

        The response data from the AJAX request.

      Returns

      {ListData}:

      An object like {data: [props, props, ...]}.

      Use

      parseListData comes in handy when dealing with an irregular API that can be improved with data transformation.

      Suppose an endpoint responds with a status of 200 OK, even when the request generates an empty result set. Worse yet, instead of representing an emtpy set with an empty list, it removes the property.

      A request to /services/todos may return:

      {
        todos: [
          {todo: {id: 0, name: "dishes"}},
          {todo: {id: 2, name: "lawn"}}
        ]
      }
      

      What if a request for /services/todos?filterName=bank responds with 200 OK:

      {
      }
      

      This response breaks its own schema. One way to bring it in line with a format compatible with ListData is:

      connect([
        require("can-connect/data/parse/parse"),
        require("can-connect/data/url/url")
      ],{
        parseListProp: "todos",
        parseListData(responseData) {
          if (responseData && !responseData.todos) {
            responseData = { todos: [] };
          }
      
          return responseData;
        }
      })
      

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