Skip to content

Commit

Permalink
Improving documentation for dv.view() (#2035)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elec0 authored Sep 15, 2023
1 parent f12261b commit a6161e6
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/docs/api/code-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,46 @@ views/custom
View scripts have access to the `dv` object (the API object), and an `input` object which is exactly whatever the second
argument of `dv.view()` was.
Bear in mind, `dv.view()` cannot read from directories starting with a dot, like `.views`. Example of an incorrect usage:
```js
await dv.view(".views/view1", { arg1: 'a', arg2: 'b' });
```
Attempting this will yield the following exception:
```
Dataview: custom view not found for '.views/view1/view.js' or '.views/view1.js'.
```
Also note, directory paths always originate from the vault root.
#### Example
In this example, we have a custom script file named `view1.js` in the `scripts` directory.
**File:** `scripts/view1.js`
```js
console.log(`Loading view1`);
function foo(...args) {
console.log('foo is called with args', ...args);
}
foo(input)
```
And we have an Obsidian document located under `projects`. We'll call `dv.view()` from this document using the `scripts/view1.js` path.

**Document:** `projects/customViews.md`
```js
await dv.view("scripts/view1", { arg1: 'a', arg2: 'b' })
```

When the above script is executed, it will print the following:

```
Loading view1
foo is called with args {arg1: 'a', arg2: 'b'}
```
## Dataviews
### `dv.list(elements)`
Expand Down

0 comments on commit a6161e6

Please sign in to comment.