All Projects β†’ rdb β†’ svelte-meteor-data

rdb / svelte-meteor-data

Licence: MIT License
Reactively track Meteor data inside Svelte components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to svelte-meteor-data

s-date-range-picker
πŸ“… A date range picker built with Svelte
Stars: ✭ 13 (-7.14%)
Mutual labels:  svelte, sveltejs, svelte-v3, svelte3, svelte-js
ui-svelte
A component library for Svelte
Stars: ✭ 18 (+28.57%)
Mutual labels:  svelte, sveltejs, svelte-v3, svelte3
svelte-interview-questions
Concepts and Questions related to Svelte - Part of official Svelte resources list
Stars: ✭ 18 (+28.57%)
Mutual labels:  svelte, sveltejs, svelte3, svelte-js
hacker-feud
πŸ’₯ A single page web game made with Svelte.
Stars: ✭ 61 (+335.71%)
Mutual labels:  sveltejs, svelte-v3, svelte3, svelte-js
Meteor-Template-helpers
Template helpers for Session, logical operations and debug
Stars: ✭ 35 (+150%)
Mutual labels:  meteor, meteor-package, meteorjs
awesome-blaze
πŸ”₯A curated list of awesome things related to Blaze
Stars: ✭ 29 (+107.14%)
Mutual labels:  meteor, meteor-package, meteorjs
svelte-inview
A Svelte action that monitors an element enters or leaves the viewport.πŸ”₯
Stars: ✭ 358 (+2457.14%)
Mutual labels:  svelte, svelte3, svelte-js
flow-router
🚦 Carefully extended flow-router for Meteor
Stars: ✭ 191 (+1264.29%)
Mutual labels:  meteor, meteor-package, meteorjs
BeatHub
Small, simple, and fast custom beatmap browser & downloader built with Svelte
Stars: ✭ 13 (-7.14%)
Mutual labels:  svelte, svelte-v3, svelte3
Meteor-logger
🧾 Meteor isomorphic logger. Store application logs in File (FS), MongoDB, or print in Console
Stars: ✭ 51 (+264.29%)
Mutual labels:  meteor, meteor-package, meteorjs
Meteor-logger-mongo
πŸƒ Meteor Logging: Store application log messages in MongoDB
Stars: ✭ 20 (+42.86%)
Mutual labels:  meteor, meteor-package, meteorjs
hypersubs
an upgraded version of Meteor subscribe, which helps optimize data and performance!
Stars: ✭ 13 (-7.14%)
Mutual labels:  meteor, meteor-package, meteorjs
Svelte Material Ui
Svelte Material UI Components
Stars: ✭ 2,081 (+14764.29%)
Mutual labels:  svelte, sveltejs, svelte3
Meteor-logger-file
πŸ”– Meteor Logging: Store application log messages into file (FS)
Stars: ✭ 24 (+71.43%)
Mutual labels:  meteor, meteor-package, meteorjs
svelte-progressbar
A multiseries, SVG progressbar component made with Svelte
Stars: ✭ 85 (+507.14%)
Mutual labels:  svelte, sveltejs, svelte-v3
svelte-portal
Svelte component for rendering outside the DOM of parent component
Stars: ✭ 261 (+1764.29%)
Mutual labels:  svelte, sveltejs, svelte3
svelte-webcomponents
A ready-to-use project template to build custom elements (web components) with Svelte 3 with support and examples for web components, jest, sass, nested components with props, eslinting, stylelinting, Github actions, propagating custom events from shadow-DOM to real-DOM etc.
Stars: ✭ 22 (+57.14%)
Mutual labels:  svelte, sveltejs, svelte3
Meteor-flow-router-title
Change document.title on the fly within flow-router
Stars: ✭ 23 (+64.29%)
Mutual labels:  meteor, meteor-package, meteorjs
svelte-color-picker
A color picker component for svelte
Stars: ✭ 96 (+585.71%)
Mutual labels:  svelte, sveltejs, svelte-v3
svelte-accessible-dialog
An accessible dialog component for Svelte apps
Stars: ✭ 24 (+71.43%)
Mutual labels:  svelte, sveltejs, svelte3

svelte-meteor-data

This package integrates the Svelte UI framework with Meteor's Tracker system. It makes it easy to write Svelte components which react automatically to changes in Meteor's data layer.

This package is still experimental. Use at your peril.

Installation

To add Svelte to your Meteor app, run:

meteor add svelte:compiler rdb:svelte-meteor-data
meteor npm install --save [email protected]

Usage

Unlike in Blaze, Svelte does not automatically become aware of changes to Meteor state, even inside $: blocks. This package provides some features that enable Svelte to become aware of such changes.

Reactive computations with useTracker

The useTracker() function can be used to expose any reactive computation as a Svelte store. You need only pass a callable returning a computed value, which will be run the first time it is used and then every time the computed value changes. The updated value is automatically made available to Svelte.

For example, this example makes the current Meteor user available in a component, and causes Svelte to update the appropriate element automatically when the current user changes:

<script>
  import { useTracker } from 'meteor/rdb:svelte-meteor-data';

  const currentUser = useTracker(() => Meteor.user());
</script>

<h1>Welcome {$currentUser.username}!</h1>

You can even mix Meteor reactivity with Svelte reactivity:

<script>
  import { useTracker } from 'meteor/rdb:svelte-meteor-data';

  let selectedUserId;

  $: selectedUser = useTracker(() => Meteor.users.findOne(selectedUserId));
</script>

<p>Selected {$selectedUser.username}</p>

Cursors

While it's possible to use queries with useTracker(() => query.fetch()), this package supports a more convenient way to handle reactive queries, by allowing you to use a MongoDB cursor directly as a Svelte store:

<script>
  export let fruitColor = 'blue';

  $: fruits = Fruits.find({color: fruitColor});
</script>

<p>Showing {$fruits.length} {fruitColor}-colored fruits:</p>
<ul>
  {#each $fruits as fruit}
    <li>{fruit.name}</li>
  {/each}
</ul>

Subscriptions

You can safely use Meteor.subscribe in your components without worrying about clean-up. The subscription will be stopped automatically when the component is destroyed.

As an added feature, you can use a subscription handle in an {#await} block:

{#await Meteor.subscribe('todos')}
  <p>Loading todos…</p>
{:else}
  <TodoList />
{/if}

Tracker.autorun

It is possible to use Tracker.autorun() with a function that is automatically re-run when its Meteor dependencies change. It will stop being updated when the component is destroyed. This will work fine for top-level computations that do not depend on any dynamic Svelte state, such as in this example:

<script>
  let currentUser;

  Tracker.autorun(() => {
    currentUser = Meteor.user();
  });
</script>

To make the autorun also respond to Svelte state changes, you need to put it under a $: block. This will work, but with some caveats: if the Tracker state is invalidated right after a change to the Svelte state, all $: blocks will be re-run. It is therefore better to use useTracker instead, as listed above.

ReactiveVar

A Meteor ReactiveVar will work seamlessly as a Svelte store, and can be accessed and bound like any writable store using the $ operator:

<script>
  import { ReactiveVar } from 'meteor/reactive-var';

  const store = new ReactiveVar("initial");
</script>

<input type="text" bind:value={$store} />

<p>Value is {$store}</p>

Session variables

If you are using Meteor Session variables, these can be exposed as a reactive Svelte store using the useSession hook. The first argument is the session key to expose, and the optional second argument allows you to set a default value for the session variable, as an added convenience.

This function is only available if the session package has been added.

<script>
  import { useSession } from 'meteor/rdb:svelte-meteor-data';

  const store = useSession('mySessionKey', 'initial');

  // The above is equivalent to:
  //Session.setDefault('mySessionKey', 'initial')
  //const store = useSession('mySessionKey');
</script>

<input type="text" bind:value={$store} />

<p>Value is {$store}</p>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].