(This isn't a question, more like a tip.. If someone found a better
way of doing this please share..)

I'm not familiar with localStorage, but i haven't found a native
function that (only) initializes the value for a certain key. Also, as
i update extensions i sometimes add new keys and default values for
them. Here's a system i've found that works and is fairly easy to use,
i'm using it in my Clock extension:


if (initStorage("v1.6", true)) {
  // v1.5:
  initStorage("hourColor", "rgba(200,0,0,1)");
  initStorage("minuteColor", "rgba(66,92,137,1)");
  initStorage("secondColor", "rgba(66,137,92,1)");
  initStorage("showDate", 1);
  initStorage("showDay", 1);

  // v1.6:
  initStorage("smallHourColor", "rgba(255,0,0,1)");
  initStorage("smallMinuteColor", "rgba(66,92,137,1)");
}

/**
* Initializes the localStorage for the given key.
* If the given key is already initialized, nothing happens.
*
* @param key The key for which to initialize
* @param initialValue Initial value of localStorage on the given key
* @return true if a value is assigned or false if nothing happens
*/
function initStorage(key, initialValue) {
  var currentValue = localStorage[key];
  if (!currentValue) {
    localStorage[key] = initialValue;
    return true;
  }
  return false;
}


When a version that adds keys will be pushed, you just need to update
the very first string, that represents the version, and add your
initStorage calls... (Repeated calls of initStorage for the same
version can be optimized, but i think it's a nice readability-
performance trade-off since that code only runs once per version) I
hope this is useful to someone =]


Teo

--

You received this message because you are subscribed to the Google Groups 
"Chromium-extensions" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/chromium-extensions?hl=en.


Reply via email to