url
Specify the url and methods that should be used for the "Data Methods".
String
If a string is provided, it's assumed to be a RESTful interface. For example, if the following is provided:
url: "/services/todos"
... the following methods and requests are used:
getListData
-GET /services/todos
getData
-GET /services/todos/{id}
createData
-POST /services/todos
updateData
-PUT /services/todos/{id}
destroyData
-DELETE /services/todos/{id}
Object
If an object is provided, it can customize each method and URL directly like:
url: {
getListData: "GET /services/todos",
getData: "GET /services/todo/{id}",
createData: "POST /services/todo",
updateData: "PUT /services/todo/{id}",
destroyData: "DELETE /services/todo/{id}"
}
You can provide a resource
property that works like providing url
as a string, but overwrite
other values like:
url: {
resource: "/services/todo",
getListData: "GET /services/todos"
}
You can also customize per-method the parameters passed to the ajax implementation, like:
url: {
resource: "/services/todos",
getListData: {
url: "/services/todos",
type: "GET",
beforeSend: () => {
return fetch('/services/context').then(processContext);
}
}
}
This can be particularly useful for passing a handler for the beforeSend
hook.
The beforeSend
hook can also be passed for all request methods. This can be useful when
attaching a session token header to a request:
url: {
resource: "/services/todos",
beforeSend: (xhr) => {
xhr.setRequestHeader('Authorization', `Bearer: ${Session.current.token}`);
}
}
Finally, you can provide your own method to totally control how the request is made:
url: {
resource: "/services/todo",
getListData: "GET /services/todos",
getData: function(param){
return new Promise(function(resolve, reject){
$.get("/services/todo", {identifier: param.id}).then(resolve, reject);
});
}
}