Created
August 5, 2016 03:27
-
-
Save riddles8888/e5348c80fcb267add8d7113bb508a818 to your computer and use it in GitHub Desktop.
store/index.js using firebase 3.x library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventEmitter } from 'events' | |
import Firebase from 'firebase' | |
// ENTER YOUR FIREBASE URL BELOW | |
const db = Firebase.initializeApp({ | |
apiKey: 'YOUR_FIREBASE_APP_SECRET', | |
databaseURL: 'https://YOUR_FIREBASE_APP.firebaseio.com/', | |
}); | |
const categoriesRef = db.database().ref().child('categories') | |
const bookmarksRef = db.database().ref().child('bookmarks') | |
const store = new EventEmitter() | |
let categories = {} | |
let bookmarks = {} | |
db.database().ref().on('value', (snapshot) => { | |
var bookmarkData = snapshot.val() | |
if (bookmarkData) { | |
categories = bookmarkData.categories | |
bookmarks = bookmarkData.bookmarks | |
store.emit('data-updated', categories, bookmarks) | |
} | |
}) | |
store.addCategory = (category) => { | |
categoriesRef.update(category) | |
} | |
store.deleteCategory = (catName) => { | |
// first check if an 'Uncategorized' category exists, if not, create it | |
if (!('Uncategorized' in categories)) { | |
categoriesRef.update({'Uncategorized': 'white'}) | |
} | |
for (var key in bookmarks) { | |
if (bookmarks[key].category === catName) { | |
bookmarksRef.child(key).update({category: 'Uncategorized'}) | |
} | |
} | |
categoriesRef.child(catName).remove() | |
} | |
store.addBookmark = (bookmark) => { | |
bookmarksRef.push(bookmark) | |
} | |
store.deleteBookmark = (bookmarkId) => { | |
bookmarksRef.child(bookmarkId).remove() | |
} | |
export default store |
Thanks very much for this! Please also note that you'll need to take the following steps in order for this to run successfully:
- Clone repository
- Update the Firebase version to 3.2.1 in your package.json
- Update the /store/index.js file as specified in above file
- Save
- npm install
- webpack
- npm start
At least that's how I got all of it working :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Diff of the changes