fishkingsin / dat-gui

Automatically exported from code.google.com/p/dat-gui
0 stars 0 forks source link

Uncaught TypeError when saving or creating a new preset #13

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create a new dat.gui
2. enable remember with the following function and properties:

amp: 0.5
brightness: 0.5
brightness_vary: 0.5
freq: 10
high: 0.9
hue: 0.5
hue_vary: 0.5
layers: 4
low: 0.34
sat: 0.5
sat_vary: 0.5
__proto__: Object

What is the expected output? What do you see instead?

all functionality works except when saving or creating a preset:..

when saving:

obj is undefined

Uncaught TypeError: Cannot read property 'forEach' of undefined
eachdat.gui.js:108
(anonymous function)dat.gui.js:2781
eachdat.gui.js:110
getCurrentPresetdat.gui.js:2772
common.extend.savedat.gui.js:2225
(anonymous function)

likewise when creating a new preset:

Uncaught TypeError: Cannot read property 'forEach' of undefined
eachdat.gui.js:108
(anonymous function)dat.gui.js:2781
eachdat.gui.js:110
getCurrentPresetdat.gui.js:2772
common.extend.saveAsdat.gui.js:2240
common.extend.width

What version of the product are you using? On what operating system?

Chrome 18 on OSX 10.7

Please provide any additional information below.

Original issue reported on code.google.com by m...@mike-tucker.com on 12 Apr 2012 at 10:54

GoogleCodeExporter commented 9 years ago
I thought I was having this problem, but the problem when the object being 
saved controls are part of a folder eg:

    obj = { test: 0};
    gui.add(obj, "test");
    gui.remember(obj); // works ok on save

    obj = { test: 0};
    var folder = gui.addFolder("folder");
    folder.add(obj, "test");
    gui.remember(obj); // fails on save

Original comment by cairns.r...@gmail.com on 4 Jul 2012 at 8:12

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I have this problem in Chrome Version 25.0.1364.160 Built on Ubuntu 12.04, 
running on LinuxMint 13 (25.0.1364.160-0ubuntu0.12.04.1)

This can be seen by clicking the Save button in Dat.gui, on the page
here: http://codepen.io/lingo/full/KuDse

Original comment by lukelett...@gmail.com on 5 May 2013 at 6:54

GoogleCodeExporter commented 9 years ago
I have this issue as well, with or without folders.
Uncaught TypeError: Cannot read property 'forEach' of undefined
dat.gui.js:110

Windows 7 64bit
Chrome and firefox.

Strangely, the workshop demo works fine.

Original comment by t...@jam3.com on 9 Aug 2013 at 11:07

GoogleCodeExporter commented 9 years ago
Sorry guys,

Just to clarify what works and what doesn't since I just discovered a quirky 
behaviour.
On the workshop demo, everything except local storage works properly.
On my own project, local storage doesn't work either, and to make matters 
worse, it throws the TypeError when I try to access the preset settings (gear 
icon), Save, or New. But if I hit Revert, the preset settings, Save and New now 
work! Local storage still doesn't work, but atleast I can grab the JSON from 
the preset settings popup and paste it into a document.

I verified this behaviour on http://codepen.io/lingo/full/KuDse
I thought a quickfix would be to run gui.revert() or gui.revert(gui) after you 
initialize your gui but it does nothing.

Cheers,

Tom

Original comment by t...@jam3.com on 9 Aug 2013 at 11:25

GoogleCodeExporter commented 9 years ago
One more update!
Apparently gui.revert() DOES work as long as you use load some JSON upon 
instantiation.

    var gui = new dat.GUI({
        load: presetJSON
    });

    gui.remember(sceneProperties);
    gui.revert();

So there you have it. As long as you manually hit the Revert button the first 
time you use the GUI to generate your initial JSON, and then using the 
load:JSON and gui.revert() trick, it works.

Original comment by t...@jam3.com on 9 Aug 2013 at 11:52

GoogleCodeExporter commented 9 years ago
SAME ISSUE!
can anyone provide a working example of presets?

Original comment by pip...@gmail.com on 6 Dec 2013 at 5:31

GoogleCodeExporter commented 9 years ago
I got it to stop bitching like this....
gui = new dat.GUI({
        load: {
            "preset": "Default",
            "closed": false,
            "remembered": {
                "Default": {}
            },
            "folders": {}
        }
    });
    gui.revert(); // stop it bitching
    gui.save(); // enable revert on Defaults
... no need to create presets first, just use the above.  If it saves to 
localStorage then it will ignore the load: value, so seems to be safe enough.

Thanks  t...@jam3.com

Original comment by cdkp...@gmail.com on 5 Jan 2014 at 1:14

GoogleCodeExporter commented 9 years ago
Guys, I just spent an hour hunting through the code and I figured it out. I 
have no idea who wrote this stuff, but it's some absurd spaghetti at parts.

***

TypeError / forEach Problem: If your controllers array is not initialized, 
you'll get the forEach error. Make sure you call remember() right after you 
instantiate the GUI object.

    var obj = { foo: 'bar' };
    var gui = new dat.GUI();
    gui.remember(obj);

I wanted to fix this bug anyway, so around Line 109 I add the following tests 
for obj existence:

    if (!obj) return;

    if (ARR_EACH && obj.forEach && obj.forEach === ARR_EACH) { 

***

LocalStorage Problem: saveToLocalStorage() is a private function for GUI and 
curiously wrapped in a block where only the getters and setters are defined.

The issue is that block is separate from the main method definitions! Thus, 
besides the getters and setters, the rest of GUI's common methods cannot call 
saveToLocalStorage(). To make matters more weird, it is never called manually. 
The function is only bound to a DOM event on window for when the tab closes... 
who the hell wrote this?

Fuck, well I tracked this down too. Around Line 1676 add:

    var saveToLocalStorage;

Then expose the private save function around Line 1930:

    saveToLocalStorage = function () {
      if (SUPPORTS_LOCAL_STORAGE && localStorage.getItem(getLocalStorageHash(this, 'isLocal')) === 'true') {
        localStorage.setItem(getLocalStorageHash(_this, 'gui'), JSON.stringify(_this.getSaveObject()));
      }
    }

    this.saveToLocalStorageIfPossible = saveToLocalStorage;

FINALLY add calls to saveToLocalStorageIfPossible() in GUI's save() and 
saveAs() methods (Line 2226):

    save: function() {

          if (!this.load.remembered) {
            this.load.remembered = {};
          }

          this.load.remembered[this.preset] = getCurrentPreset(this);
          markPresetModified(this, false);          

          this.saveToLocalStorageIfPossible();
        },

After it all, you'll get LocalStorage + presets working just fine.

Attached is the patched file. Good god, I can't believe how much time I just 
spent on that.

Original comment by drewlus...@gmail.com on 23 Jan 2014 at 7:04

Attachments:

GoogleCodeExporter commented 9 years ago
Nice work! Frankly, I still use this library but only for its GUI. The 
saving/loading I now do manually with JSON and  a mongoDB; not as simple, but 
has the advantage of collaborative presets.

Original comment by t...@jam3.com on 23 Jan 2014 at 2:58

GoogleCodeExporter commented 9 years ago
Fixed this and merged it to a maintained github-hosted fork of dat.gui 

https://github.com/dataarts/dat.gui/commit/f36a8273a1162559a24b9d574aeb7c5112342
9ea

Original comment by drewlus...@gmail.com on 23 Jan 2014 at 11:24