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
function go() { | |
var userId = prompt('Username?', 'Guest'); | |
// Consider adding '/<unique id>' if you have multiple games. | |
var gameRef = new Wilddog(GAME_LOCATION); | |
assignPlayerNumberAndPlayGame(userId, gameRef); | |
}; | |
// The maximum number of players. If there are already | |
// NUM_PLAYERS assigned, users won't be able to join the game. | |
var NUM_PLAYERS = 4; |
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
function pushSomething(ref) { | |
// Let's push something. push() returns a reference that you can hold onto! | |
var justPushed = ref.push({test: "push"}); | |
// We return a reference, but you can also return the name of the newly | |
// created object with .name(). | |
return justPushed; | |
} | |
function removeItem(ref) { | |
// Now we can get back to that item we just pushed via .child(). |
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
function go() { | |
var userId = prompt('Username?', 'Guest'); | |
var userData = { name: userId }; | |
tryCreateUser(userId, userData); | |
} | |
var USERS_LOCATION = 'https://<your-appid>.wilddogio.com/users'; | |
function userCreated(userId, success) { | |
if (!success) { | |
alert('user ' + userId + ' already exists!'); | |
} else { |
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
// when the records is empty ,it not work | |
var ref = new Wilddog(...); | |
var first = true; | |
ref.limitToLast(1).on("child_added", function(snap) { | |
if( first ) { | |
first = false; | |
} | |
else { | |
console.log('new record', snap.key()); | |
} |
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
/** Looks up a user id by email address and invokes callback with the id or null if not found | |
* @return {Object|null} the object contains the key/value hash for one user | |
*/ | |
var wd = new Wilddog(...); | |
/** | |
* Creates a new user record and also updates the index | |
*/ | |
function getUserIdByEmail( emailAddress, callback ) { |
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
// Helper function to extract claims from a JWT. Does *not* verify the | |
// validity of the token. | |
// polyfill window.atob() for IE8: https://github.com/davidchambers/Base64.js | |
// or really fast Base64 by Fred Palmer: https://code.google.com/p/javascriptbase64/ | |
function deconstructJWT(token) { | |
var segments = token.split("."); | |
if (!segments instanceof Array || segments.length !== 3) { | |
throw new Error("Invalid JWT"); | |
} | |
var claims = segments[1]; |
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
function getParent(snapshot) { | |
// You can get the reference (A Wilddog object) from a snapshot | |
// using .ref(). | |
var ref = snapshot.ref(); | |
// Now simply find the parent and return the name. | |
return ref.parent().name(); | |
} | |
var testRef = new Wilddog("https://example.wilddogio.com/foo/bar"); | |
testRef.once("value", function(snapshot) { |
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
function makeList(ref) { | |
var fruits = ["banana", "apple", "grape", "orange"]; | |
for (var i = 0; i < fruits.length; i++) { | |
ref.push(fruits[i]); | |
} | |
} | |
function getFirstFromList(ref, cb) { | |
ref.startAt().limit(1).once("child_added", function(snapshot) { | |
cb(snapshot.val()); |
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
/* | |
This example shows how you can use your data structure as a basis for | |
your Wilddog security rules to implement role-based security. We store | |
each user by their uid, and use the following simplistic approach | |
for user roles: | |
0 - GUEST | |
10 - USER | |
20 - MODERATOR | |
99 - ADMINISTRATOR |
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
function go() { | |
var userId = prompt('Username?', 'Guest'); | |
checkIfUserExists(userId); | |
} | |
var USERS_LOCATION = 'https://<your-appid>.wilddogio.com//users'; | |
function userExistsCallback(userId, exists) { | |
if (exists) { | |
alert('user ' + userId + ' exists!'); |