can.fixture.types.requestHandler
Specifies the response of a fixture. Used in can.fixture.
Parameters
-
The ajax settings object that
was passed to can.ajax or a jQuery ajax method. Any templated
portions of the url passed to can.fixture are added as
data to request.data
. For example, calling:
$.get("/todos/5");
With the following fixture:
can.fixture("/todos/:id", function(request, response){ })
request.data.id
will be 5
.
-
response
{responseHandler(status, statusText, responses, headers)}
Optional
Optionally called to specify the response of the fixture.
-
requestHeaders
{Object<String,String>}
A map of request headers specified by can.ajax's headers property.
Returns
{* | undefined}
If a value is returned, it is used as a JSON
response body. If nothing is returned, it's expected that responseHandler
was used.
Use
requestHandler
functions are passed to can.fixture like:These functions are called with:
request
- The options provided to the ajax method, unmodified, and thus, without defaults from ajaxSettings.response
- The response callback.headers
- A map of key/value request headers.Specify the result of the Ajax request by either returning the result within the
requestHandler
or calling response.Returning the result directly
Return the data result of an Ajax request directly within a
requestHandler
. For example:Calling response to specify the result
The response callback passed to a
requestHandler
can be used to specify all values in a response. For example:The response callback can also be called with just a single JSON object for the common case where you want a successful response with JSON data:
If you want to return an array of data respond like this: