Created
July 31, 2015 20:10
-
-
Save katowulf/00c9b0a804dfe2b25da4 to your computer and use it in GitHub Desktop.
Wrap a Firebase Query to include a toString with the parameters.
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
// Not all API methods are implemented in this proof | |
// See https://www.firebase.com/docs/web/api/query/ | |
var FirebaseQueryWrapper = (function() { | |
function FirebaseQueryWrapper(url) { | |
this.ref = new Firebase(url); | |
this.parms = {}; | |
} | |
var proto = FirebaseQueryWrapper.prototype; | |
proto.on = function() { | |
return this.ref.on.apply(this.ref, arguments); | |
}; | |
proto.once = function() { | |
return this.ref.once.apply(this.ref, arguments); | |
}; | |
proto.toString = function() { | |
var args = getArgsList(this.parms); | |
return this.ref.toString() + (args.length? '?' + args.join('&') : ''); | |
}; | |
proto.orderByChild = meta('orderByChild'); | |
proto.orderByPriority = meta('orderByPriority'); | |
proto.orderByKey = meta('orderByKey'); | |
proto.orderByValue = meta('orderByValue'); | |
proto.limitToFirst = meta('limitToFirst'); | |
proto.limitToLast = meta('limitToLast'); | |
proto.startAt = meta('startAt'); | |
proto.endAt = meta('endAt'); | |
proto.equalTo = meta('equalTo'); | |
function meta(name) { | |
return function(x) { | |
this.ref = this.ref[name](x); | |
this.parms[name] = x; | |
return this; | |
} | |
} | |
function getArgsList(parms) { | |
var args = []; | |
for(var key in parms) { | |
if( parms.hasOwnProperty(key) ) { | |
args.push(key + '=' + encodeURIComponent(parms[key])); | |
} | |
} | |
return args; | |
} | |
return FirebaseQueryWrapper; | |
})(); |
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
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>Muck Around</title> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script> | |
<script type="text/javascript" src="https://cdn.firebase.com/js/client/2.2.9/firebase.js"></script> | |
<script type="text/javascript" src="tmp.js"></script> | |
<script type="text/javascript"> | |
var query = new FirebaseQueryWrapper('https://kato-books.firebaseio.com/') | |
.orderByChild('date').limitToFirst(5); | |
console.log(query.toString()); | |
query.on('value', function(snap) { | |
console.log(snap.val()); | |
}); | |
</script> | |
</head> | |
<body> | |
<p>Open the JavaScript console.</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment