Last active
November 10, 2015 20:40
-
-
Save amark/c9c45b756f5528b92960 to your computer and use it in GitHub Desktop.
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
<html> | |
<body> | |
<script src="http://rawgit.com/amark/gun/master/gun.js"></script> | |
<script> | |
localStorage.clear(); // reset the data! | |
Gun.chain.joinTeam = function(team){ // extends gun to make bidirectional pointing easy! | |
var member = this; | |
member.val(function(theMember){ | |
team.set(theMember); // link the member to the team. | |
}); | |
team.val(function(theTeam){ | |
member.path('team').put(theTeam); // link the team to the member. | |
}) | |
return member; | |
} | |
var gun = Gun(); | |
var snakes = gun.get('team/snakes').set(); // an empty set makes sure this team exists. | |
var mongooses = gun.get('team/mongooses').set(); // same as above. | |
var alice = gun.put({name: "alice"}).joinTeam(mongooses); | |
var bob = gun.put({name: "bob"}).joinTeam(snakes); | |
var carl = gun.put({name: "carl"}).joinTeam(mongooses); | |
// who is on the mongooses team? | |
mongooses.map().val(function(theMember){ | |
console.log(theMember, "on mongooses team!"); | |
}); | |
// who is on alice's team? | |
alice.path('team').map().val(function(theMember){ | |
console.log(theMember, "on alice's team!"); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment