JSFiddle - React, Tailwind, and code Playground
by Anid Monsur
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.2.5/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/1.1.1/angularfire.min.js"></script>
<script src="https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.min.js"></script>
<div ng-app='sample' ng-controller='TestCtrl as vm'>
<h4>Specific Object</h4>
<button type='button' ng-click='vm.fireUpdate()'>AngularFire Update</button>
<button type='button' ng-click='vm.normalUpdate()'>Normal Update</button>
<button type='button' ng-click='vm.update()'>Update</button>
<h5>{{ vm.message }}</h5>
<pre>{{ vm.data | json }}</pre>
<h4 style='margin-top: 40px;'>All Data</h4>
<pre>{{ vm.all | json }}</pre>
</div>
JavaScript
angular.module('sample', ['firebase'])
.controller('TestCtrl', function ($firebaseObject, $firebaseArray, $timeout) {
var vm = this;
var fb = new Firebase('https://bb-testing.firebaseio.com');
var ref = new Firebase.util.NormalizedCollection(fb.child('path1'), fb.child('path2'))
.select('path1.foo', 'path2.bar')
.ref();
var normObject = $firebaseObject(ref.child('-Js7BlzA8CT9VewjE9yg'));
vm.fireUpdate = function () {
normObject.foo = parseInt(Math.random() * 1000);
normObject.$save().then(function () {
vm.message = 'Object saved';
}, function (e) {
console.log('AngularFire update error: ' + e);
vm.message = 'Save failed with error, see console';
});
};
vm.normalUpdate = function () {
ref.child('-Js7BlzA8CT9VewjE9yg').child('foo').set(parseInt(Math.random() * 1000),
function (e) {
console.log('Normal update error: ' + e);
if (e) {
$timeout(function () {
vm.message = 'Normal update failed';
});
} else {
$timeout(function () {
vm.message = 'Normal update succeeded';
});
}
});
};
vm.update = function () {
fb.child('path1').child('-Js7BlzA8CT9VewjE9yg').child('foo').set(parseInt(Math.random() * 1000),
function (e) {
console.log('Update error: ' + e);
if (e) {
$timeout(function () {
vm.message = 'Update failed';
});
} else {
$timeout(function () {
vm.message = 'Update succeeded';
});
}
});
};
vm.data = normObject;
vm.all = $firebaseObject(fb);
});