Skip to content

Instantly share code, notes, and snippets.

var authClient = new FirebaseAuthClient(db, function(error, user) {
if (error) {
alert(error);
} else if (user) {
$(body).removeClass("noAuth").addClass("auth");
@katowulf
katowulf / precise_ordered_records.js
Last active December 20, 2015 10:20
Using a simple update counter to maintain precise ordering of records in Firebase
var sequenceRef = new Firebase(...+'/entries');
var updateCounter = new Firebase(...+'/counter');
updateCounter.transaction(function(currentValue) {
return currentValue+1;
}, function(error, committed, snap) {
if( error ) { ... }
else if( committed ) {
var updateCounter = snap.val();
sequenceRef.push().setWithPriority({
@katowulf
katowulf / curl.sh
Last active December 23, 2015 14:22
Vikrum's mighty CURL command
curl -so /dev/null -w "url_effective = %{url_effective}\n http_code = %{http_code}\n http_connect = %{http_connect}\n time_total = %{time_total}\n time_namelookup = %{time_namelookup}\n time_connect = %{time_connect}\n time_appconnect = %{time_appconnect}\n time_pretransfer = %{time_pretransfer}\n time_redirect = %{time_redirect}\n time_starttransfer = %{time_starttransfer}\n size_download = %{size_download}\n size_upload = %{size_upload}\n size_header = %{size_header}\n size_request = %{size_request}\n speed_download = %{speed_download}\n speed_upload = %{speed_upload}" https://<your instance>.firebaseio.com/some/deep/path.json
@katowulf
katowulf / push_to_urbarairship.js
Last active February 16, 2016 13:58
Node.js process to queue from Firebase to Urban Airship.
// UNTESTED; THEORETICAL, based on http://docs.urbanairship.com/connect/ios_push.html
https://github.com/mikeal/request
var request = require('request');
/** Presumably the snapshot will contain something like this:
*
{
"audience": {"device_token": "FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660"},
"notification": {
@katowulf
katowulf / module.simpleLoginTools.js
Last active March 3, 2016 04:26
A service and several directives based on ng-cloak, which wait for angularFire to finish authenticating--rather than just until Angular bootstraps--before displaying it's content. Handy to get rid of those blips that display when user isn't logged in--oh, wait, now they are.
'use strict';
/**
* This module monitors angularFire's authentication and performs actions based on authentication state.
* directives/directive.ngcloakauth.js depends on this file
*
* Modify ng-cloak to hide content until FirebaseSimpleLogin resolves. Also
* provides ng-show-auth methods for displaying content only when certain login
* states are active.
*
@katowulf
katowulf / global.js
Last active May 30, 2016 21:27
Overriding error handling in AngularFire by extending and decorating $$error
// this will remove all error logging globally
angular.config(function($provide) {
$provide.decorator("$firebaseObject", function($delegate) {
$delegate.prototype.$$error = function(err) {
this.$destroy(err);
};
return $delegate;
});
$provide.decorator("$firebaseArray", function($delegate) {
@katowulf
katowulf / oldnew.js
Last active June 3, 2016 01:50
Include old/new values in $firebaseArray::$watch method.
angular.factory('Arr', function($firebaseArray) {
function Arr(ref) {
this.oldVals = {};
return $firebaseArray.call(this, ref);
}
Arr.prototype.$$process = function(event, rec, prevKey) {
$firebaseArray.prototype.$$process.apply(this, arguments);
this.oldVals[rec.$id] = rec;
};
@katowulf
katowulf / descending_order_firebasearray.js
Created June 10, 2016 18:25
Descending sorted array in AngularFire, accomplished by unshift() instead of push().
app.factory('DescendingArray', function($firebaseArray) {
return $firebaseArray.$extend({
// Modify $$process to handle added events by prepending instead of appending
$$process: function(event, rec, prevChild) {
var key = this.$$getKey(rec);
var changed = false;
if( event === 'child_added' ) {
curPos = this.$indexFor(key);
@katowulf
katowulf / diffList.js
Last active July 28, 2016 18:42
Diff services for monitoring objects and arrays, and creating changelists in Angular.js
/**
* A diff utility that compares arrays and returns a list of added, removed, and updated items
*
* Returns an object with two methods:
* diff: do a one-time diff of two arrays
* watch: observe a variable on scope and report any changes to a callback
*
* Invoking the factory is done like so:
* <code>
* function(listDiff) {
@katowulf
katowulf / push_notifications.js
Last active December 2, 2016 23:53
Push notifications from Firebase to Twilio.
//require the Twilio module and create a REST client
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');
var Firebase = require('firebase');
var fb = new Firebase('FIREBASE_URL');
fb.auth('FIREBASE_SECRET', function(err) {
if( err ) { throw err; }
listenForEvents();
})