Skip to content

Instantly share code, notes, and snippets.

@amark
Last active November 10, 2015 20:40
Show Gist options
  • Save amark/c9c45b756f5528b92960 to your computer and use it in GitHub Desktop.
Save amark/c9c45b756f5528b92960 to your computer and use it in GitHub Desktop.
<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