npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuex-ignite

v1.0.2

Published

Firebase bindings for Vuex

Downloads

8

Readme

Vuex Ignite

Easy to use Vuex Firebase bindings inspired by posva/vuexfire and vuejs/vuefire.

Installation

Install with yarn or npm:

npm install vuex-ignite

yarn add vuex-ignite

...or with a script tag:

<script src="https://unpkg.com/vuex-ignite"></script>

Usage

First ignite the store by importing the ignite function and wrapping the store options with it.

import { ignite } from 'vuex-ignite';

export default new Vuex.Store(ignite({
    state: {
        counter: 0
    },
    mutations: {
        incrementCounter(state) {
            state.counter++;
        }
    }
}));

If you've used a script tag instead, the ignite function will be available as window.VuexIgnite.ignite.

This automatically adds the necessary Firebase mutations and action along with a plugin that will automatically dispatch that action when the store is ready to set everything up.

The ignite function will read information about how you want to bind data from the firebase object in the store options. The simplest way is to provide the name of which to bind to and a reference to the data.

import { ignite } from 'vuex-ignite';
import * as firebase from 'firebase';

const app = firebase.initializeApp({ ... });
const db = app.database();

export default new Vuex.Store(ignite({
    firebase: {
        tasks: db.ref('myTasks')
    }
}));

In a Vue component:

<template>
    <ul>
        <li v-for="task in tasks" :key="task['.key']">{{task}}</li>
    </ul>
</template>

<script>
    import { mapState } from 'vuex';

    export default {
        computed: {
            ...mapState(['tasks'])
        }
    }
</script>

Notice that the key for the list item is available as the .key property.

By default, this will bind the reference as an array to a new tasks array in the store's state. Querying is also supported, meaning you could replace db.ref('myTasks') with db.ref('myTasks').orderByValue() for instance.

If you didn't want to bind tasks as an array but as an object instead, you must replace the definition with something like this:

firebase: {
    tasks: {
        ref: db.ref('settings'),
        type: Object
    }
}

You can also replace the type with String, Boolean, Number, or Array to give a nicer default value. Similarly to .key, if the value is primitive, the value will be stored in .value.

User Based Bindings

Now let's imagine that the tasks reference needs to be based on the currently authenticated Firebase user. Instead of explicitly defining the database references, we can define them as functions that return references. These functions take the current user as their first parameter. Here's an example:

const auth = app.auth();

...

firebase: {
    user: auth,
    tasks: {
        ref: user => db.ref(`${user.uid}/tasks`),
        type: Array
    }
}

Every time the authentication state changes this reference will be redetermined and bound to tasks.

Notice that we also define a user property and bind the Firebase Auth instance to it. This doesn't have to be called user, but it does have to be included for these functional bindings to work. This has the side effect of granting access to the currently authenticated user in the store or anywhere else in your application by accessing the user object in the store's state.

Updating Data

Every time a reference is bound to the store's state, a corresponding $firebaseRef variable is set in the store allowing data to be changed easily, even if it's user dependent.

<template>
    <button @click="addTask"></button>
</template>

<script>
    import { mapState } from 'vuex';

    export default {
        methods: {
            addTask() {
                this.$store.$firebaseRefs.tasks.push("New task");
            }
        }
    }
</script>