Coder Social home page Coder Social logo

vuexfire's Introduction

VuexFire Build Status npm package coverage size

SSR ready Firebase binding for Vuex

Supports only Vue 2, Vuex 2 and Firebase 2/3 If you need an older version check the v1 branch: npm i -D vuexfire@v1

Installation

  1. Using a CDN:
<script src="https://unpkg.com/vuexfire"></script>
  1. In module environments, e.g CommonJS:
npm install vue firebase vuexfire --save

Usage

Add the mutations to your root Store and make sure to define the property you want to bind in the state first:

import { firebaseMutations } from 'vuexfire'
const store = new Vuex.Store({
  state: {
    todos: [], // Will be bound as an array
    user: null // Will be bound as an object
  },
  mutations: {
    // your mutations
    ...firebaseMutations
  }
})

It works with modules as well, but you don't need to add the mutations there:

const store = new Vuex.Store({
  modules: {
    todos: {
      state: {
        todos: [], // Will be bound as an array
        user: null // Will be bound as an object
      },
    }
  }
})

In order to use VuexFire, you have to enhance actions. This action enhancer takes the actual action and enhances it with two additional parameters in the context, bindFirebaseRef and unbindFirebaseRef:

import { firebaseAction } from 'vuexfire'

const setTodosRef = firebaseAction(({ bindFirebaseRef, unbindFirebaseRef }, { ref }) => {
  // this will unbind any previously bound ref to 'todos'
  bindFirebaseRef('todos', ref)
  // you can unbind it easily too
  unbindFirebaseRef('todos')
})

Access it as a usual piece of the state:

const Component = {
  template: '<div>{{ todos }}</div>',
  computed: Vuex.mapState(['todos']),
  created () {
    this.$store.dispatch('setTodosRef', db.ref('todos'))
  }
}

Browser support

VuexFire requires basic WeakMap support, which means that if you need to support any of these browsers:

  • IE < 11
  • Safari < 7.1
  • Android < 5.0

You'll have to include a polyfill. You can use Benvie/WeakMap

You can find more information about WeakMap support here

How does it work?

VuexFire uses multiple global mutations prefixed by vuexfire/ to call the actual mutations to modify objects and arrays. It listens for updates to your firebase database and commits mutations to sync your state. Thanks to the action enhancer firebaseAction, it gets access to the local state and commit so it works with modules too 👍

Examples

You can check out the examples by opening the html files in your browser, or check this online Demo

API

firebaseMutations

This object contains VuexFire internal mutations. They are all prefixed by vuexfire/. This object must be added in the root Store mutations object.

bindFirebaseRef(key, ref[, options])

Only available inside of an enhanced action

Binds a firebase reference to a property in the state. If there was already another reference bound to the same property, it unbinds it first.

options:

{
  cancelCallback: Function, // Cancel callback passed to Firebase when listening for events
  readyCallback: Function, // Callback called once the data has been loaded. Useful for SSR
  errorCallback: Function, // Callback called when there is an error loading the data. Useful for SSR
  wait: Boolean, // (Arrays only) Should Vuexfire wait for the whole array to be populated. Defaults to true
}

wait can be set to true every time. It's useful to do pagination and SSR.

unbindFirebaseRef(key)

Only available inside of an enhanced action

Unbinds a bound firebase reference to a given property in the state.

License

MIT

Support on Beerpay

Hey dude! Help me out for a couple of 🍻!

Beerpay Beerpay

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.