Created
June 26, 2014 01:11
-
-
Save greggnakamura/2551befd65dd8ce939b1 to your computer and use it in GitHub Desktop.
AngularJS, Firebase: Querying
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
<h2>{{ title }}</h2> | |
<div class="jumbotron"> | |
<div ng-repeat="message in messages"> | |
{{ message.user }} - {{ message.text }} | |
</div> | |
</div> | |
<div class="row marketing"> | |
<input type="text" class="form-control" ng-model="currentUser" /> | |
<div class="input-group"> | |
<input type="text" class="form-control" ng-model="currentText" /> | |
<span class="input-group-btn"> | |
<button class="btn btn-default" type="button" ng-click="sendMessage()">Send</button> | |
</span> | |
</div> | |
</div> |
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
/*global Firebase*/ | |
'use strict'; | |
angular.module('firebaseApp') | |
.controller('MainCtrl', function ($scope, $timeout) { | |
var rootRef = new Firebase('https://glaring-fire-877.firebaseio.com/'); | |
var messagesRef = rootRef.child('messages'); | |
var titleRef = rootRef.child('title'); | |
// scope variables | |
$scope.title = null; | |
$scope.currentUser = null; | |
$scope.currentText = null; | |
$scope.messages = []; | |
// Pull from Firebase one time using 'once' | |
titleRef.once('value', function(snapshot) { | |
$scope.title = snapshot.val(); | |
}); | |
// 'child_added' to get new child added to node | |
// using 'child_added' on first load, gets all items in node, then any new child add to node | |
messagesRef.on('child_added', function (snapshot) { | |
$timeout(function () { | |
var snapshotVal = snapshot.val(); | |
//console.log(snapshotVal); | |
// get name of node when updated | |
$scope.messages.push({ | |
text: snapshotVal.text, | |
user: snapshotVal.user, | |
name: snapshot.name() | |
}); | |
}); | |
}); | |
// on child changed | |
messagesRef.on('child_changed', function (snapshot) { | |
$timeout(function () { | |
var snapshotVal = snapshot.val(); | |
var message = findMessageByName(snapshot.name()); | |
// update message text | |
message.text = snapshotVal.text; | |
}); | |
}); | |
// on child removed | |
messagesRef.on('child_removed', function (snapshot) { | |
$timeout(function () { | |
deleteMessageByName(snapshot.name()); | |
$('input').val(""); | |
}); | |
}); | |
function findMessageByName(name){ | |
var messageFound = null; | |
for(var i = 0; i < $scope.messages.length; i++){ | |
var currentMessage = $scope.messages[i]; | |
if(currentMessage.name === name){ | |
messageFound = currentMessage; | |
break; | |
} | |
} | |
return messageFound; | |
} | |
function deleteMessageByName(name){ | |
for(var i = 0; i < $scope.messages.length; i++){ | |
var currentMessage = $scope.messages[i]; | |
if(currentMessage.name === name){ | |
$scope.messages.splice(i,1); | |
break; | |
} | |
} | |
} | |
$scope.sendMessage = function() { | |
// declare object | |
var newMessage = { | |
user: $scope.currentUser, | |
text: $scope.currentText | |
}; | |
// push 'newMessage' object to Firebase | |
messagesRef.push(newMessage); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment