GithubHelp home page GithubHelp logo

vuexfire's Introduction

Vuexfire Build Status npm package coverage

Firebase binding for Vuex

This is heavily inspired from vuefire.

Supports:

  • Vue 1/2 with Vuex 1
  • Vue 2 with Vuex 2

Installation

  1. If included as global <script>: will install automatically if global Vue is present.
<head>
  <!-- Vue -->
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <!-- Vuex -->
  <script src="https://unpkg.com/vuex/dist/vuex.js"></script>
  <!-- Firebase -->
  <script src="https://gstatic.com/firebasejs/3.5.2/firebase.js"></script>
  <!-- VuexFire -->
  <script src="https://unpkg.com/vuexfire/dist/vuexfire.js"></script>
</head>
  1. In module environments, e.g CommonJS:
npm install vue vuex firebase vuexfire --save
var Vue = require('vue')
var Vuex = require('vuex')
var VuexFire = require('vuexfire')
var Firebase = require('firebase')

// explicit installation required in module environments
Vue.use(Vuex)
Vue.use(VuexFire)

Usage

When binding

Vuex v1

var store = new Vuex.Store({
  state: {
    items: null // items must be declared on the state
  }
},
// actions, mutations, etc
)

new Vue({
  el: '#app',
  store: store,
  vuex: {
    getters: {
      items: function (state) { return state.items }
    }
  },
  firebase: {
    items: db.ref('items') // bind as an array
  }
})

Vuex v2

Setup mutations

var store = new Vuex.Store({
  state: {
    items: null
  },
  mutations: VuexFire.mutations,
  getters: {
    items: function (state) { return state.items }
  }
})

new Vue({
  el: '#app',
  store: store,
  computed: Vuex.mapGetters([
    'items'
  ]),
  firebase: {
    items: db.ref('items')
  }
})

Modules

In larger applications you may consider splitting up your store into modules. If that's your case you can use module with vuexfire by using a dot separated key like cart.items or user.cart.items. You must use the moduleMutations() method to generate the mutations for your module

  // Define a module
  var cart = {
    state: {
      items: null // Initialize the variable
    },
    // Getters receive the cart module state only
    getters: {
      lastItem: function (state) {
        return state.items[state.items.length - 1]
      },
      items: function (state) { return state.items }
    },
    mutations: VuexFire.moduleMutations('cart') // This is the name given to the store
  }

  // Create the store
  var store = new Vuex.Store({
    modules: {
      cart: cart
    }
  }

Everything else works just as vuefire. Refer to its readme for more documentation.

Examples

You can checkout the examples by serving an http-server at the root of this project.

Support on Beerpay

Hey dude! Help me out for a couple of ๐Ÿป!

Beerpay Beerpay

vuexfire's People

Contributors

greenkeeper[bot] avatar npmcdn-to-unpkg-bot avatar posva avatar psimyn avatar tazer avatar

Watchers

 avatar  avatar

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.