Skip to content

Instantly share code, notes, and snippets.

@yimengtianya
yimengtianya / player_assignment.js
Last active November 7, 2015 09:57
Wilddog:在多人游戏中使用事务分配玩家
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;
@yimengtianya
yimengtianya / remove_pushed.js
Last active November 7, 2015 09:58
Wilddog:联合使用push和remove方法删除数据
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().
@yimengtianya
yimengtianya / create.js
Last active November 7, 2015 09:56
Wilddog:使用事务创建新用户,如果新增的数据不存在则创建,否则返回已经存在的数据
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 {
@yimengtianya
yimengtianya / limit_endat.js
Last active February 25, 2018 05:33
Wilddog:只获取新增的数据
// 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());
}
@yimengtianya
yimengtianya / using_an_index.js
Created October 15, 2015 03:07
Wilddog:根据邮箱地址查找用户id
/** 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 ) {
@yimengtianya
yimengtianya / jwtparser.js
Created October 13, 2015 11:50
Wilddog:解析jwt token
// 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];
@yimengtianya
yimengtianya / wilddog_snapshot_parent.js
Created October 13, 2015 11:39
Wilddog: 得到父引用快照.
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) {
@yimengtianya
yimengtianya / wilddog_first_item.js
Created October 13, 2015 11:35
Wilddog: 获取list中的第一个数据
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());
@yimengtianya
yimengtianya / data-structure.js
Last active February 13, 2017 01:58
Wilddog基于角色的安全策略规则
/*
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
@yimengtianya
yimengtianya / wilddog_detect_data.js
Last active February 13, 2017 01:54
Wilddog: 检测用户数据是否存在
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!');