how to get the whole list object variables and arrays without need to get from object child id gundb?

915 Views Asked by At

I just notice that object child of object has id but not that I wanted that when I try to get the object variable. I wanted what store in object of object but I found id only?

For example I used scene manage the objects to save and load for easy access.

{ x:0, y:0, z:0, prarms:{height:0,width:0} }

result from save is when I check the data I found it different.

{ x:0, y:0, z:0, prarms:#randomids }

I used set and put if the objects matches using the uuid. Need a bit of help on how it get it working. Wanted to get the whole object data but not the gundb object ids.


Manage to get partly working code for set and get data object to work partly. Here simple version how to set.

//console.log("saving object data????");
        this.check_gunsceneobj(obj['uuid'],(bfind,id)=>{
            //console.log("....CALLS");
            var gscene = this.gun.get('scene');
            //check child keys var is object to put var
            function gunObjectAssign(_gun, _obj){
                for(var i in _obj){
                    if(typeof _obj[i] == 'object'){
                        console.log(i);
                        //pathing for object child of object
                        _gun.path(i).put(_obj[i]);
                        gunObjectAssign(_gun.path(i),_obj[i]);
                    }
                }
            }
            if(bfind){
                console.log("set object scene[update]");
                if(id !=null){
                    console.log(id);
                    gscene.path(id).put(obj);
                    gunObjectAssign(gscene.path(id),obj);
                }
            }else{
                console.log("save object scene[insert]");
                //console.log(obj);
                gscene.set(obj);
            }
            console.log("object save?");
        });

here the get objects.

function gunObjDataAssign(self,data){
                    for(var i in data){
                        if(typeof data[i] === 'object'){
                            if(data[i] !=null){
                                var id = data[i]['#'];
                                data[i] = {}; //clear id hash
                                self.get(id).val((objdata)=>{
                                    delete objdata._;
                                    data[i] = objdata;
                                    gunObjDataAssign(self,objdata);
                                });
                            }
                        }
                    }
                }

                Gun.chain.valueobj = function (cb, opt) {
                  return this.val(function (val, field) {
                      if(val !=null){
                          delete val._;
                      }
                      gunObjDataAssign(this,val);
                      cb.call(this, val, field);
                  }, opt);
                };

                Gun.chain.liveobj = function (cb, opt) {
                  return this.on(function (val, field) {
                    delete val._;
                    gunObjDataAssign(this,val);
                    cb.call(this, val, field);
                  }, opt);
                };

                Gun.chain.eachobj = function () {
                  var each = this.map();
                  return this.valueobj.apply(each, arguments);
                };

Have not fully tested. But it worked party not try detect any update once push to the scene objects.

Used Sample Code from snippets.

1

There are 1 best solutions below

0
On

If I understand your question correctly... you probably want to use this plugin:

https://github.com/gundb/synchronous

gun.get('player').sync({}, {}, function(player){
  // player updated!
  console.log("Full object, not just pointers:", player);
})

There is also https://github.com/gundb/onward which gives you the diff and path on the document.

Does this help? Let me know and I'll revise the answer as necessary.