Last active
December 21, 2016 07:40
-
-
Save msenel/5e4179b2e78c20968b7102b2bc95f645 to your computer and use it in GitHub Desktop.
Firebase SDK javascript write examples
This file contains 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
var database = firebase.database(); | |
//var channel = new Channel('First Channel', 'First Channel Description', '#myfirstchannel', 'true', 'hQcWCU0smxVUNNDf9YHMl5f65sr1'); | |
//var channel2 = new Channel('Second Channel', 'Second Channel Description', '#mysecondchannel', 'true', 'hQcWCU0smxVUNNDf9YHMl5f65sr1'); | |
var user = new User('Mike', 'mike@example.com'); | |
var userId = createUser(user); | |
var channel = new Channel(user.displayName + '\'s Channel', 'Channel Description', '#' + user.displayName + 'channel', 'true', userId); | |
saveChannel(channel); | |
function saveChannel(channel) { | |
// Get a key for a new Post. | |
var newChannelKey = database.child('channels').push().key; | |
var myChannels = {}; | |
myChannels[newChannelKey] = true; | |
// Write the new post's data simultaneously in the channels list and the user's myChannels list. | |
var updates = {}; | |
updates['/channels/' + newChannelKey] = channel; | |
updates['/users/' + channel.ownerId + '/myChannels'] = myChannels; | |
database().ref().update(updates) | |
.then(function() { | |
console.log('Synchronization succeeded'); | |
}) | |
.catch(function(error) { | |
console.log('Synchronization failed'); | |
});; | |
} | |
saveUser(user) { | |
var userListRef = database.ref('users').push(); | |
userListRef.set(user) | |
.then(function() { | |
console.log('Synchronization succeeded'); | |
}) | |
.catch(function(error) { | |
console.log('Synchronization failed'); | |
});; | |
return userListRef.key; | |
} | |
function Channel(title, description, hashtag, type, ownerId) { | |
this.title = title; | |
this.description = description; | |
this.hashtag = hashtag; | |
this.public = type; | |
this.ownerId = ownerId; | |
} | |
function User(displayName, email) { | |
this.displayName = displayName; | |
this.email = email; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment