getData()
Description
Gets a promise with arrays of tasks, links and optionally resources and assignmentsThe getData() method is a part of the RestDataProvider service intended for working with a server
Usage
getData(
id?: string | number,
options?: { resources?: boolean, assignments?: boolean }
): Promise<{
tasks: [],
links: [],
resources?: [],
assignments?: []
}>
Parameters
id- (optional) the id of a task whose branch should be loaded; if omitted, the whole dataset is loadedoptions- (optional) an object that enables loading of additional collections together with tasks and links:resources- (optional) if set to true, resources are loaded viaGET /resources. Applied only on initial load (whenidis omitted).assignments- (optional) if set to true, assignments are loaded. On initial load, the request isGET /assignments; on subtree load, the request isGET /assignments/{id}and returns the assignments scoped to the requested branch.
Response
The getData() method sends requests to the server by the GET method and returns a promise with data on tasks and links (and resources/assignments when requested via options). For the examples of returned objects, see tasks, links, resources and assignments.
Examples
Loading tasks and links only:
<script>
import { Gantt } from "@svar-ui/svelte-gantt";
import { RestDataProvider } from "@svar-ui/gantt-data-provider";
const url = "https://some_backend_url";
const server = new RestDataProvider(url);
let tasks = $state(), links = $state();
server.getData().then(data => {
tasks = data.tasks;
links = data.links;
});
</script>
<Gantt
{tasks}
{links} />
Loading tasks, links, resources and assignments:
<script>
import { Gantt } from "@svar-ui/svelte-gantt";
import { RestDataProvider } from "@svar-ui/gantt-data-provider";
const server = new RestDataProvider("https://some_backend_url");
let tasks = $state(), links = $state(), resources = $state(), assignments = $state();
server
.getData(undefined, { resources: true, assignments: true })
.then(data => {
tasks = data.tasks;
links = data.links;
resources = data.resources;
assignments = data.assignments;
});
</script>
<Gantt
{tasks}
{links}
{resources}
{assignments} />
Loading a subtree together with its assignments (use inside a request-data handler when tasks are marked lazy):
<script>
import { Gantt } from "@svar-ui/svelte-gantt";
import { RestDataProvider } from "@svar-ui/gantt-data-provider";
const restProvider = new RestDataProvider("https://some_backend_url");
let api = $state(), tasks = $state(), links = $state(),
resources = $state(), assignments = $state();
restProvider
.getData(undefined, { resources: true, assignments: true })
.then(data => {
tasks = data.tasks;
links = data.links;
resources = data.resources;
assignments = data.assignments;
});
function init(api) {
api.setNext(restProvider);
api.on("request-data", ev => {
restProvider
.getData(ev.id, { assignments: true })
.then(({ tasks, links, assignments }) => {
api.exec("provide-data", {
id: ev.id,
data: { tasks, links, assignments },
});
});
});
}
</script>
<Gantt {init} {tasks} {links} {resources} {assignments} bind:this={api} />
parseDates() method
Description: getData() applies the parseDates() method to parse dates (start, end, base_start, base_end from tasks) to Date object (as new Date(string)).
Usage:
parseDates(task) => task
If you need to change the parsing method, you can redefine it as in the example below: create a new class for RestDataProvider, set the required parsing method using the parseDates() method, and then apply this new class for RestDataProvider.
class MyProvider extends RestDataProvider{
parseDates(task){
// parse dates by method you like
return task;
}
}
const restProvider = new MyProvider(serverUrl);
Related articles: