gun/function GUN(){
2016-03-07 18:17:23 -08:00

52 lines
1.2 KiB
Plaintext

function GUN(){
if(!(this instanceof GUN)){ return new GUN() }
}
GUN.chain = GUN.prototype;
GUN.chain.get = function(soul){
var gun = GUN();
gun._ = {soul: soul};
gun.back = function(lex, fn, cb){
var at = {node: localStorage[lex.soul], lex: gun._};
cb(at); fn(at);
}
return gun;
}
GUN.chain.path = function(field){
var gun = GUN(), from = this;
gun._ = {field: field};
gun.back = function(lex, fn, cb){
from.back({field: field, soul: from._.soul}, function(at){
at.lex = gun._;
cb(at); fn(at);
}, cb);
}
return gun;
}
GUN.chain.put = function(data){
var gun = GUN(), from = this, put = {data: data};
from.back(gun._ = {}, function(at){
if(!at.node){
put.root = at.put.root || {};
if(at.lex.field){
(at.put.node || put.root)[at.lex.field] = put.data;
}
}
localStorage[put.root['#']] = JSON.stringify(put.root);
}, function(at){
at.put = at.put || {};
if(!at.put.root){ at.put.root = at.put.at = {'#': at.lex.soul} }
if(at.lex.field){
at.put.at = (at.put.node = at.put.at)[at.lex.field] = {};
}
});
return gun;
}
var i = 1, gun = GUN();
gun.get('users').path(i).path('where').put({
lat: Math.random(), lng: Math.random(), i: i
})