> Just use any JSON specification compliant library (like
http://www.json.org/json2.js
> from Douglas himself), get/set the cookies using parse/stringify
> methods, and use identical comparison to "undefined" and voilà, you
> have state management which is independent of what is actually
> persisted in the cookies.
Thanks Diogo, but I'm past that. I used json2.js for months as a quick
solution, but I hate JS dependancies, so in my lastest version I wrote
the stringify() subroutine shown above. This allowed me to completely
replaced json2.js for my needs.
My motivation for posting the idea here was: A) So in the future I
could use core UI functionality instead of custom code, and B) because
storing/retrieving JSON seems the best solution for *all* UI state-
management . Using a JSON cookie makes restoring state simple: $.extend
(options, $.cookie("widgetOrInstanceName"));
Because my widget has multiple objects with multiple states, I allow
users to customize which states are persisted by specifying them in
the options. They can also persist multiple instances of the widget by
naming each instance...
options = {
autoStateManagement: true
, cookie: {
name: ""
, autoSave: true // Save a state cookie when page exits?
, autoLoad: true // Load the state cookie when Layout inits?
, keys: "north.size,south.size,east.size,west.size,"+
"north.isClosed,south.isClosed,east.isClosed,west.isClosed,"+
"north.isHidden,south.isHidden,east.isHidden,west.isHidden"
}
}
All standard cookie options can also be set: expires, path, domain &
secure.
I expose methods to read/clear/save/load state so custom code can be
used to manage state instead:
getCookie() // read the cookie
clearState() // delete the cookie
loadState() // update options with saved state
getState(keys) // create JSON object of all 'state keys', ready to
save
saveState(keys) // write state to a cookie
The concept is easily adapted as a *generic state-management solution*
for any UI widget. The missing piece is a standard way to save/
retrieve JSON to/from a cookie. That's why I piped up when I saw meta-
data and cookies being discussed together - seemed like a perfect
match. Even though I have a working solution, I'd like to see the UI
core implement JSON cookies and/or other persistence methods --
eventually.
/Kevin