[whatwg] Make DOMStringMap constructable, and el.dataset writeable?

Currently, el.dataset is readonly.  A friend of mine passed along a
use-case for making it writeable.

She's making a game, where the game initialization script expects
certain types of elements, and for the initial state data of the
elements to be present in data attributes on them.  She'd like to be
able to pause the game, shifting all the state data into localStorage,
then later resume by pulling it back onto elements before starting the
game script.

Right now she has to do this with a for-in loop, like:

for(var i = 0; i < cards.length; i++) {
  for(var k in carddata[i]) {
    cards[i].dataset[k] = carddata[i][k];
  }
}

It would be somewhat cleaner if she could simply construct a
DOMStringMap and assign it, like so:

for(var i = 0; i < cards.length; i++) {
  cards[i].dataset = new DOMStringMap(carddata[i]);
}

Another potentially interesting use-case for this is making it
possible to "transfer" data-* attributes from one element to another
with a simple "el1.dataset = el2.dataset;" statement.

Thoughts?

~TJ

Received on Friday, 30 November 2012 19:24:53 UTC