项目作者: gdg-tangier

项目描述 :
:cloud: Cloud Firestore binding in realtime with Vuejs
高级语言: JavaScript
项目地址: git://github.com/gdg-tangier/vue-firestore.git
创建时间: 2017-10-21T16:24:29Z
项目社区:https://github.com/gdg-tangier/vue-firestore

开源协议:MIT License

下载









vue-firestore

Vue.js binding for firebase cloud firestore.

Prerequisites

Firebase ^7.6.1

Try it out: Demo

Installation

Globally (Browser)

vue-firestore will be installed automatically.

  1. <!-- Vue -->
  2. <script src="https://unpkg.com/vue"></script>
  3. <!-- Firebase -->
  4. <script src="https://www.gstatic.com/firebasejs/5.3.1/firebase.js"></script>
  5. <!-- Firestore -->
  6. <script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-firestore.js"></script>
  7. <!-- vue-firestore -->
  8. <script src="https://unpkg.com/vue-firestore"></script>
  9. <script>
  10. // Firebase config.
  11. var config = {
  12. apiKey: "your-apik-ey",
  13. authDomain: "your-auth-domain",
  14. databaseURL: "your-database-url",
  15. projectId: "your-project-id",
  16. storageBucket: "your-storage-bucket",
  17. messagingSenderId: "your-messaing-sender-id"
  18. }
  19. // Initialize Firebase.
  20. firebase.initializeApp(config);
  21. </script>

npm

Installation via npm : npm install vue-firestore --save

Usage

vue-firestore supports binding for the both (collections/docs) in realtime, you can bind your properties in two ways using firestore option or bind them manually with $binding.

  1. using firestore option.
  1. import Vue from 'vue'
  2. import VueFirestore from 'vue-firestore'
  3. import Firebase from 'firebase'
  4. require('firebase/firestore')
  5. Vue.use(VueFirestore)
  6. var firebaseApp = Firebase.initializeApp({ ... })
  7. const firestore = firebaseApp.firestore();
  8. var vm = new Vue({
  9. el: '#app',
  10. firestore () {
  11. return {
  12. // Collection
  13. persons: firestore.collection('persons'),
  14. // Doc
  15. ford: firestore.collection('cars').doc('ford')
  16. }
  17. }
  18. })

You can pass an object to the firestore() function.

As you may know, firestore source returns a promise, so you can handle it if it’s resolved by resolve function
or rejected by reject function, this case is really useful when we want to wait for data to be rendered and do some specific actions.

  1. firestore () {
  2. return {
  3. persons: {
  4. // collection reference.
  5. ref: firestore.collection('persons'),
  6. // Bind the collection as an object if you would like to.
  7. objects: true,
  8. resolve: (data) => {
  9. // collection is resolved
  10. },
  11. reject: (err) => {
  12. // collection is rejected
  13. }
  14. }
  15. }
  16. }
  1. Manually binding

You can bind your docs/collection manually using this.$binding, and wait for data to be resolved, this case is really useful when we want to wait for data to be rendered and do some specific actions.

  1. ...
  2. mounted () {
  3. // Binding Collections
  4. this.$binding("users", firestore.collection("users"))
  5. .then((users) => {
  6. console.log(users) // => __ob__: Observer
  7. })
  8. // Binding Docs
  9. this.$binding("Ford", firestore.collection("cars").doc("ford"))
  10. .then((ford) => {
  11. console.log(ford) // => __ob__: Observer
  12. }).catch(err => {
  13. console.error(err)
  14. })
  15. }
  16. ...

Vue firestore latest release supports binding collections as objects, you can bind the collection manually by this.$bindCollectionAsObject(key, source) or you can explicitly do that by adding {objects: true} to firestore() function, see the previous example above.

The normalized resutls of $bindCollectionAsObject:

  1. {
  2. tjlAXoQ3VAoNiJcka9: {
  3. firstname: "Jhon",
  4. lastname: "Doe"
  5. },
  6. fb7AcoG3QAoCiJcKa9: {
  7. firstname: "Houssain",
  8. lastname: "Amrani"
  9. }
  10. }

You can get access to firestore properties with this.$firestore.

Adding Data to collections

  1. var vm = new Vue({
  2. el: '#app',
  3. firestore: function () {
  4. return {
  5. persons: firestore.collection('persons')
  6. }
  7. },
  8. methods:{
  9. addData: function () {
  10. this.$firestore.persons.add({
  11. firstname: "Amrani",
  12. lastname: "Houssain"
  13. })
  14. }
  15. }
  16. })

Each record of the array will contain a .key property which specifies the key where the record is stored.

The Result of persons collection will be normalized as :

  1. [
  2. {
  3. ".key": "-Jtjl482BaXBCI7brMT8",
  4. "firstname": "Amrani",
  5. "lastname": "Houssain"
  6. },
  7. {
  8. ".key": "-JtjlAXoQ3VAoNiJcka9",
  9. "firstname": "John",
  10. "lastname": "Doe"
  11. }
  12. ]

You could delete or update a json document of a collection using the property .key of a given object.

  1. // Vue methods
  2. deletePerson: function (person) {
  3. this.$firestore.persons.doc(person['.key']).delete()
  4. },
  5. updatePerson: function (person) {
  6. this.$firestore.persons.doc(person['.key']).update({
  7. name: "Amrani Houssain"
  8. github: "@amranidev"
  9. })
  10. }

You can customize the name of the .key property by passing an option when initializing vue-firestore:

  1. require('firebase/firestore')
  2. Vue.use(VueFirestore, {
  3. key: 'id', // the name of the property. Default is '.key'.
  4. enumerable: true // whether it is enumerable or not. Default is true.
  5. })

This would allow you to do person.id instead of person['.key'].

More Resources

LICENSE

MIT