dataarts/dat.gui — pull request #33 by drewlustro (2014-01-23 07:42:52+00:00)
Title: Fixed forEach iterator TypeError, made localStorage usage easier

>This pull request fixes **[two bugs causing grief on the google-hosted repo of dat.gui](https://code.google.com/p/dat-gui/issues/detail?id=13)**.

### No Controllers Yet TypeError

People including myself have been getting *TypeError*'s thrown by `each(obj, iter, scope)` in `utils/common.js`. This occurs if you try to save a preset and happened to call `gui.remember()` too late *after* creating controllers.

Instead of an exception being the behavior, I added a condition test to `obj` and `obj.forEach` to see if either the tested object or iterator exist before attempting `.forEach()`.

### Unreliable LocalStorage

The localStorage save feature only fires as a DOM event on window close, which seems kind of ridiculous. The fix was even harder to hunt down, because the `saveToLocalStorage()` function is private and only exposed within the scope of declaring GUI's getters and setters. GUI's methods can't even call it!

I have no idea why this is the case.

I exposed the `saveToLocalStorage()` method via `GUI.saveToLocalStorageIfPossible()` and added calls to it within `GUI.save()` and `GUI.saveAs()`, because if the feature is enabled, I would consider saving to localStorage upon clicking either to be "expected behavior".