Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions site/source/api/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,18 @@ Defines a [helper function](#Template-helpers) which can be used from all templa

The [template instance](#Template-instances) corresponding to the current template helper, event handler, callback, or autorun. If there isn't one, `null`.

In an `async` function, call `Template.instance()` before the first `await`: once the function resumes, Blaze no longer tracks the current template instance and `Template.instance()` returns `null`. Event handlers receive the instance as their second argument and lifecycle callbacks have it as `this`, so capture it in a variable first:

```js
Template.editor.events({
async 'click .save'(event, instance) {
const saved = await Meteor.callAsync('drafts.save', instance.draft.get());
// `Template.instance()` would be `null` here
instance.draft.set(saved);
},
});
```

### Template.currentData() {#Template-currentData}

- Inside an `onCreated`, `onRendered`, or `onDestroyed` callback, returns
Expand Down
22 changes: 22 additions & 0 deletions site/source/guide/reusable-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,25 @@ Template.Lists_show_page.onRendered(function () {
});
});
```

## Web Components

Custom elements work in a Blaze template like any other element: Blaze creates them with `document.createElement`,
so they upgrade once their definition is registered, their attributes follow your data reactively, and the events
they dispatch are reachable from event maps like any DOM event.

Attributes are strings. A custom element that takes structured data (an array, an object, a function) exposes it as
a JavaScript property, and a property cannot be set from the template: <code v-pre>&lt;my-chart data={{points}}&gt;</code> gives the
element the attribute `data="[object Object]"`. Set the property from JavaScript instead, in an `autorun` started
from `onRendered()`, so that it follows the data:

```js
Template.Dashboard.onRendered(function () {
this.autorun(() => {
this.find('my-chart').data = Template.currentData().points;
});
});
```

The `autorun` re-runs when `points` changes and stops when the template instance is destroyed. The same pattern
applies to native properties that have no attribute form, such as `video.srcObject`.
29 changes: 29 additions & 0 deletions site/source/guide/understanding-blaze.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ If your helper or sub-component is expensive to run, and often re-runs without a
unnecessary re-runs by using a more subtle reactive data source. The [
`peerlibrary:computed-field`](https://atmospherejs.com/peerlibrary/computed-field) package helps achieve this pattern.

## Flushing before the browser looks at the DOM

Changing a reactive data source does not update the DOM on the spot: Blaze re-runs the affected helpers and updates
the DOM at the next Tracker flush, which happens asynchronously after the current code has finished. Code that
reads the DOM right after a change sees the old state; `Tracker.afterFlush(callback)` runs the callback once the
DOM reflects the change.

Some browser APIs take a callback and expect the DOM to be updated by the time it returns. The View Transitions
API is one of them: `document.startViewTransition(callback)` captures the old state, runs the callback, captures
the new state and animates between the two. If the callback only changes a reactive variable, the DOM is still
unchanged when the new state is captured, and nothing animates. Call `Tracker.flush()` at the end of the callback to
apply the pending updates synchronously:

```js
Template.gallery.events({
'click .next'(event, instance) {
const update = () => {
instance.current.set(instance.current.get() + 1);
Tracker.flush();
};
if (document.startViewTransition) {
document.startViewTransition(update);
} else {
update();
}
},
});
```

## Attribute helpers

Setting tag attributes via helpers (e.g. `<div {{attributes}}>`) is a neat tool and has some
Expand Down
6 changes: 6 additions & 0 deletions site/source/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ While this is a minor release, removing jQuery is breaking for code that relied
`event.target`, `event.currentTarget`, `preventDefault()`, `stopPropagation()` and
`stopImmediatePropagation()` work the same; jQuery-only helpers such as `isDefaultPrevented()` are
not available, use `event.defaultPrevented` instead.
- Event properties that jQuery does not copy onto its event object, such as `dataTransfer` (drag and
drop), `deltaY` (wheel), `clipboardData` (copy and paste), `inputType` (input events),
`animationName`, `newState` (popover toggle) or `command` (invoker commands), were only reachable
through `event.originalEvent`. With the native backend they are on `event` itself, and
`event.originalEvent` is `undefined`. Code that has to run on both backends can read
`(event.originalEvent || event).dataTransfer`.
- jQuery plugins called on `template.$(...)` no longer work, since there is no jQuery object to call
them on.

Expand Down
Loading