mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
Merge pull request #253 from alancnet/set-usability
Enable .set with node, or new soul.
This commit is contained in:
commit
1242e4b971
16
gun.js
16
gun.js
@ -527,6 +527,12 @@
|
|||||||
|
|
||||||
Gun.on.at.copy = function(at){ return Gun.obj.del(at, 'hash'), Gun.obj.map(at, function(v,f,t){t(f,v)}) }
|
Gun.on.at.copy = function(at){ return Gun.obj.del(at, 'hash'), Gun.obj.map(at, function(v,f,t){t(f,v)}) }
|
||||||
|
|
||||||
|
Gun.root = function(gun) {
|
||||||
|
if (!Gun.is(gun)) return null;
|
||||||
|
if (gun.back === gun) return gun;
|
||||||
|
return Gun.root(gun.back);
|
||||||
|
};
|
||||||
|
|
||||||
}(Gun));
|
}(Gun));
|
||||||
|
|
||||||
;(function(Gun){ // Gun prototype chain methods.
|
;(function(Gun){ // Gun prototype chain methods.
|
||||||
@ -1000,8 +1006,16 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
Gun.chain.set = function(item, cb, opt){
|
Gun.chain.set = function(item, cb, opt){
|
||||||
var gun = this, ctx = {}, chain;
|
var gun = this, ctx = {}, chain, rel;
|
||||||
cb = cb || function(){};
|
cb = cb || function(){};
|
||||||
|
if(Gun.is.node(item) && (rel=Gun.is.rel(item._))){
|
||||||
|
// Resolve set with a node
|
||||||
|
return gun.set(gun.get(rel), cb, opt);
|
||||||
|
}
|
||||||
|
if(typeof(item)=='object'&&!Gun.is(item)&&!Gun.is.rel(item)&&!Gun.is.lex(item)&&!Gun.is.node(item)&&!Gun.is.graph(item)){
|
||||||
|
// Resolve set with a new soul
|
||||||
|
return gun.set(Gun.root(gun).put(item), cb, opt);
|
||||||
|
}
|
||||||
if(!Gun.is(item)){ return cb.call(gun, {err: Gun.log('Set only supports node references currently!')}), gun } // TODO: Bug? Should we return not gun on error?
|
if(!Gun.is(item)){ return cb.call(gun, {err: Gun.log('Set only supports node references currently!')}), gun } // TODO: Bug? Should we return not gun on error?
|
||||||
(ctx.chain = item.chain()).back = gun;
|
(ctx.chain = item.chain()).back = gun;
|
||||||
ctx.chain._ = item._;
|
ctx.chain._ = item._;
|
||||||
|
@ -3757,9 +3757,18 @@ describe('Gun', function(){
|
|||||||
bob.path('friends').set(alice);
|
bob.path('friends').set(alice);
|
||||||
dave.path('friends').set(alice).back.set(carl);
|
dave.path('friends').set(alice).back.set(carl);
|
||||||
|
|
||||||
|
// Test set with new object
|
||||||
|
var alan = users.set({name: 'alan', birth: Math.random()}).key('person/alan');
|
||||||
|
alan.val(function(alan) {
|
||||||
|
// Test set with node
|
||||||
|
dave.path('friends').set(alan);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
var team = gun.get('team/lions').put({name: "Lions"});
|
var team = gun.get('team/lions').put({name: "Lions"});
|
||||||
team.path('members').set(alice);
|
team.path('members').set(alice);
|
||||||
team.path('members').set(bob);
|
team.path('members').set(bob);
|
||||||
|
team.path('members').set(alan);
|
||||||
|
|
||||||
alice.path('team').put(team);
|
alice.path('team').put(team);
|
||||||
bob.path('team').put(team);
|
bob.path('team').put(team);
|
||||||
@ -3771,10 +3780,14 @@ describe('Gun', function(){
|
|||||||
} else
|
} else
|
||||||
if('bob' === member.name){
|
if('bob' === member.name){
|
||||||
done.bob = true;
|
done.bob = true;
|
||||||
} else {
|
} else
|
||||||
|
if('alan' === member.name){
|
||||||
|
done.alan = true;
|
||||||
|
} else
|
||||||
|
{
|
||||||
expect(member).to.not.be.ok();
|
expect(member).to.not.be.ok();
|
||||||
}
|
}
|
||||||
if(done.alice && done.bob){
|
if(done.alice && done.bob && done.alan){
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
done();
|
done();
|
||||||
},10);
|
},10);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user