data/url
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:
- URL values can include simple templates like
{id}
that replace that part of the URL with values in the data passed to the method. - GET and DELETE request data is put in the URL using param.
- 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. - 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.