W3C home > Mailing lists > Public > whatwg@whatwg.org > November 2012

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

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Fri, 30 Nov 2012 11:23:53 -0800
Message-ID: <CAAWBYDDVSJreETHcqSuyJ9CRwu8X7tmufCnws=0J2UUmX+NfqQ@mail.gmail.com>
To: WHATWG List <whatwg@whatwg.org>
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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 30 January 2013 18:48:11 GMT