|
| 1 | +# `svelte-meteor-data` |
| 2 | + |
| 3 | +This package integrates the [Svelte](https://svelte.dev) UI framework with |
| 4 | +Meteor's Tracker system. It makes it easy to write Svelte components which |
| 5 | +react automatically to changes in Meteor's data layer. |
| 6 | + |
| 7 | +This package is still experimental. Use at your peril. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +To add Svelte to your Meteor app, run: |
| 12 | + |
| 13 | +```bash |
| 14 | +meteor add svelte:compiler rdb:svelte-meteor-data |
| 15 | +meteor npm install --save [email protected] |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Unlike in Blaze, Svelte does not automatically become aware of changes to Meteor |
| 21 | +state, even inside `$:` blocks. This package provides some features that enable |
| 22 | +Svelte to become aware of such changes. |
| 23 | + |
| 24 | +### Reactive computations with `useTracker` |
| 25 | + |
| 26 | +The `useTracker()` function can be used to expose any reactive computation as a |
| 27 | +Svelte store. You need only pass a callable, which will be run the first time |
| 28 | +it is used and then every time the computed value changes. The changed value is |
| 29 | +automatically made available to Svelte. |
| 30 | + |
| 31 | +For example, this makes the current Meteor user available in a component, and |
| 32 | +causes Svelte to update the appropriate element automatically when the current |
| 33 | +user changes: |
| 34 | + |
| 35 | +```svelte |
| 36 | +<script> |
| 37 | + const currentUser = useTracker(() => Meteor.user()); |
| 38 | +</script> |
| 39 | +
|
| 40 | +<h1>Welcome {$currentUser.username}!</h1> |
| 41 | +``` |
| 42 | + |
| 43 | +You can even mix Meteor reactivity with Svelte reactivity: |
| 44 | + |
| 45 | +```svelte |
| 46 | +<script> |
| 47 | + let selectedUserId; |
| 48 | +
|
| 49 | + $: selectedUser = useTracker(() => selectedUserId); |
| 50 | +</script> |
| 51 | +
|
| 52 | +<p>Selected {$selectedUser.username}</p> |
| 53 | +``` |
| 54 | + |
| 55 | +### Cursors |
| 56 | + |
| 57 | +While it's possible to use queries with `useTracker(() => query.fetch())`, this |
| 58 | +package supports a more convenient method, directly treating the cursor as a |
| 59 | +Svelte store: |
| 60 | + |
| 61 | +```svelte |
| 62 | +<script> |
| 63 | + export let fruitColor = 'blue'; |
| 64 | +
|
| 65 | + $: fruits = Fruits.find({color: fruitColor}); |
| 66 | +</script> |
| 67 | +
|
| 68 | +<p>Showing {$fruits.length} {fruitColor}-colored fruits:</p> |
| 69 | +<ul> |
| 70 | + {#each $fruits as fruit} |
| 71 | + <li>{fruit.name}</li> |
| 72 | + {/each} |
| 73 | +</ul> |
| 74 | +``` |
| 75 | + |
| 76 | +### Subscriptions |
| 77 | + |
| 78 | +You can safely use `Meteor.subscribe` in your components without worrying about |
| 79 | +clean-up. The subscription will be stopped automatically when the component is |
| 80 | +destroyed. |
| 81 | + |
| 82 | +As an added feature, you can use a subscription handle in an `{#await}` block: |
| 83 | + |
| 84 | +```svelte |
| 85 | +{#await Meteor.subscribe('todos')} |
| 86 | + <p>Loading todos…</p> |
| 87 | +{:else} |
| 88 | + <TodoList /> |
| 89 | +{/if} |
| 90 | +``` |
| 91 | + |
| 92 | +### `Tracker.autorun` |
| 93 | + |
| 94 | +It is possible to use `Tracker.autorun()` to have code automatically be re-run |
| 95 | +when its Meteor dependencies change. It will stop running when the component is |
| 96 | +destroyed. This will work fine for top-level computations that do not depend on |
| 97 | +any dynamic Svelte state, such as this example: |
| 98 | + |
| 99 | +```svelte |
| 100 | +<script> |
| 101 | + let currentUser; |
| 102 | +
|
| 103 | + Tracker.autorun(() => { |
| 104 | + currentUser = Meteor.user(); |
| 105 | + }); |
| 106 | +</script> |
| 107 | +``` |
| 108 | + |
| 109 | +However, it will not automatically detect changes to Svelte state, nor can I |
| 110 | +guarantee that it will work well with `$:`, so I highly recommend the use of |
| 111 | +`useTracker` instead. |
| 112 | + |
| 113 | +### ReactiveVar |
| 114 | + |
| 115 | +A Meteor ReactiveVar will work seamlessly as a Svelte store, and can be accessed |
| 116 | +and bound like any writable store using the `$` operator: |
| 117 | + |
| 118 | +```svelte |
| 119 | +<script> |
| 120 | + import { ReactiveVar } from 'meteor/reactive-var'; |
| 121 | +
|
| 122 | + const store = new ReactiveVar("initial"); |
| 123 | +</script> |
| 124 | +
|
| 125 | +<input type="text" bind:value={$store} /> |
| 126 | +
|
| 127 | +<p>Value is {$store}</p> |
| 128 | +``` |
0 commit comments