Skip to content

Commit 3c4478d

Browse files
committed
update examples for new API
1 parent 89d8853 commit 3c4478d

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed
Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,71 @@
11
---
2-
text: aasdasd
3-
locked: false
2+
text: abcasdasd
3+
locked: true
44
---
55

66
Locked: `INPUT[toggle:locked]`
77
```js-engine
88
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
9-
10-
const signal = mb.createSignal(undefined);
11-
12-
component.register(mb.listenToMetadata(signal, context.file.path, ['locked']));
13-
9+
// a component for lifecycle management of the fields created in our render function
1410
const comp = new obsidian.Component(component);
1511
16-
function render() {
12+
// create a bind target to the property that we care about
13+
const bindTarget = mb.bindTargetParser.fromStringAndValidate('locked', context.file.path);
14+
15+
// the render function, it takes the locked value as the argument
16+
function render(value) {
17+
// first we unload the component to unload the content from the previous rerender
1718
comp.unload();
19+
// then we load the component again for this rerender.
1820
comp.load();
21+
// then we empty the element we render to to remove content created in the previous rerender
1922
container.empty();
23+
24+
// next we create the field based on the locked value
2025
let field;
21-
if (signal.get()) {
26+
if (value) {
2227
field = mb.createInlineFieldFromString("VIEW[{text}][text]", context.file.path, undefined);
2328
} else {
2429
field = mb.createInlineFieldFromString("INPUT[text:text]", context.file.path, undefined);
2530
}
31+
32+
// and finally we render that field
2633
mb.wrapInMDRC(field, container, comp);
2734
}
2835
29-
const reactive = engine.reactive(render);
30-
signal.registerListener({
31-
callback: () => reactive.refresh(),
32-
});
36+
// we create a reactive component from the render function and the initial value will be the value of the bind target
37+
const reactive = engine.reactive(render, mb.getMetadataWithBindTarget(bindTarget));
38+
39+
// then we subscribe to the metadata that the bind target points to and rerender the reactive component everythime the bind target value changes
40+
const subscription = mb.subscribeToMetadataWithBindTarget(
41+
bindTarget,
42+
(value) => reactive.refresh(value)
43+
);
44+
45+
// don't forget to unregister the subscription when this code block unloads
46+
component.register(() => subscription.unsubscribe());
3347
3448
return reactive;
3549
```
3650

3751
```js-engine
3852
const mb = engine.getPlugin('obsidian-meta-bind-plugin').api;
3953
40-
const signal = mb.createSignal(undefined)
41-
component.register(mb.listenToMetadata(signal, context.file.path, ['text']))
42-
4354
function onUpdate(value) {
44-
return value.toString()
55+
return value.toString();
4556
}
4657
47-
const reactive = engine.reactive(onUpdate, signal.get())
48-
signal.registerListener({
49-
callback: (v) => reactive.refresh(v),
50-
})
58+
const reactive = engine.reactive(onUpdate, context.metadata.frontmatter.text);
59+
60+
const subscription = mb.subscribeToMetadata(
61+
'frontmatter',
62+
context.file.path,
63+
['text'],
64+
false,
65+
(value) => reactive.refresh(value)
66+
);
67+
68+
component.register(() => subscription.unsubscribe());
5169
5270
return reactive;
5371
```

exampleVault/Buttons/Replace Buttons.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
### Two buttons that replace the same text
22

3-
some
3+
other
44
text
5-
wow
5+
tada
66

77
```meta-bind-button
88
label: Replace in Note
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
Templater Buttons
2-
Templater Buttons
3-
Templater Buttons
4-
51

62
```meta-bind-button
73
label: Insert Text
@@ -11,8 +7,7 @@ tooltip: ""
117
id: ""
128
style: default
139
action:
14-
type: "insertIntoNote"
15-
line: 1
16-
value: "templates/templater/Templater Template.md"
10+
type: "replaceSelf"
11+
replacement: "templates/templater/Templater Template.md"
1712
templater: true
1813
```

0 commit comments

Comments
 (0)