Inside Ex DOM Storage
Ex DOM Storage ã®ä¸ã®å®è£ ã§ãããçµæ§è¦å´ããã®ã§æè§ã ãã解説ãã¡ãããçãªã¨ã³ããªã§ãã
ãªã®ã§èå³ã®ãã人以å¤ã«ã¯ã ãã¶ãããã§ãw
Ex DOM Storage ã®æè¡çæ¦è¦³
ç°¡æ½ã«æ¸ãã¦ãã¾ãã¨ä»¥ä¸ã®äºç¹ã«ã¤ãã¾ãã
- ãã¼ã¿ã®æ ¼ç´å 㯠userData behavior
- Storage ãªãã¸ã§ã¯ãã®å®æ 㯠script è¦ç´ ã«é©ç¨ãã element behavior
userData behavior ã¨ã¯ï¼
userData Behavior ã¨ã¯ãIE ãæ¨æºã§ç¨æãã¦ããã¯ã©ã¤ã¢ã³ããµã¤ãã¹ãã¬ã¼ã¸ã§ãã
使ãæ¹ã¯é常ã«ç°¡åã§ã
element.addBehavior("#default#userData"); element.load("myStorage"); element.setAttribute("myData", "blah blah"); element.save("myStorage");
ã®ããã«ããã¨ãelement ã® myData ã¨è¨ãå±æ§ã«å¯¾ãã¦ãã¼ã¿ãæ°¸ç¶çã«ä¿ååºæ¥ã¾ãã
userData behavior ããã¡ã¤ã³åä½ã§ã®ä¿åãªã®ã§ãä¿åå ã¯ããã使ã£ã¦ãã¾ãã
Storage ãªãã¸ã§ã¯ãã®ããããã£ã«å¯¾ããä»£å ¥ã¨ onstorage ã¤ãã³ã
ããããã£ã¸ã®ä»£å ¥ã®æ¤åº
ãããã Storage ãªãã¸ã§ã¯ãã«ã¯ getItem(key), setItem(key, newValue) ã¨è¨ãã¡ã½ãããããããã¡ããå¾åã«å©ãã¦ããããªãåé¡ã¯ç¡ãã£ãã®ã§ããã
sessionStorage.hoge = "fuga";
ã¨è¨ããããªããã ã®ä»£å ¥ã«å¯¾ãã¦ã onstorage ã¤ãã³ãã fire ããã®ã§ãããç´ ã® JS ã§ã¯ãããå®ç¾ããæã ã¦ãç¡ãã§ãã
Object#watch() ãããªãè¿ãã§ãããIE ã® JScript ã«ã¯ç¡ãããwatch ã¯ããããæå®ããããããã£ã¸ã®ä»£å ¥ãç£è¦ãªã®ã§ãä»»æã®ããããã£ã«å¯¾ãã¦ã¯ã©ãã«ããªãã¾ããã
ããã§æãã¤ããã®ã onpropertychange イベントãã¤ã¾ã Storage ãªãã¸ã§ã¯ãã®å®æ ã HTMLElement ã«ãã¦ãã¾ãã°è£è¶³åºæ¥ãããã¨ã
å ·ä½çã«ã¯ã
element.attachEvent("onpropertychange", function(evt) { alert("key: " + evt.key); alert("newValue: " + element[key]); });
ã®ããã«åå¾åºæ¥ãæãã§ãã
Behavior ã§ä½ã£ãã«ã¹ã¿ã ã¤ãã³ãã®å¶ç´
onstorage ã¤ãã³ãã§ãããå½å㯠Behavior ã§ã«ã¹ã¿ã ã¤ãã³ãã使ã£ã¦ã¾ãããã¤ã¾ããhtcã§
<public:event id="storegeEvent" name="onstorage" />
ã®ããã«ãã¦ããã¦ã
var evt = document.createEventObject(); // evt ã®ããããã£ã¨ãè¨å® storageEvent.fire(evt);
ãªæãã§ãã£ã¦ãã®ã§ãããã«ã¹ã¿ã ã¤ãã³ãã£ã¦ã¤ãã³ããããªã³ã°ããªããããã®è¦ç´ ã®onstorageå±æ§ã«ç´æ¥ãã³ãã©ãå®ç¾©ããªãã¨ææåºæ¥ãªãã¨è¨ãå¶ç´ãããã¾ãã*1
ããã«è¿½ãæã¡ãããã話ã§ãdocument.createEventObject() ã§ä½ã£ãã¤ãã³ãã® type ããããã£ã®å¤ã IE ã«ã¨ã£ã¦æªç¥ã®å¤ (ä¾ãã° storage ã¨è¨ãå¤) ã ã¨ãã¯ã attachEvent åºæ¥ãªãã¨è¨ãç½ ã
ããã¯æ©ãã æ«ã« attachEvent ã hack ããã¨è¨ããå¤å° evil ãªææ³ãåã£ã¦ã¾ãããã®å²ãåãã¯äºææ§éè¦ã®ããã§ãã
HTC ã«ãã¬ã³ããªã³ã°ã¢ã¼ãããã
document.compatMode ã®å¤ã§ãããã§ãããäºæã¢ã¼ãã®å ´åã 㨠onpropertychange ã¤ãã³ããæ±ããªãã®ã§ãhtc ã®åé ã« DOCTYPE 宣è¨ãä»ãã¦ã¾ãããããå°å³ã ãã©åãéè¦ã
sessionStorage ã®å®è£
ãã©ã¦ã¶ãéããã¨æ¶ãã ã¨è¨ãæåãå®ç¾ããçºã«ãcookie ã使ã£ã¦ã¾ãããã㯠userData ã§ã¯ã©ãã«ãåºæ¥ãªãäºãå¤æããã®ã§è¦èã®çã§ãã
length, remainingSpace ããããã£ã®å®è£
ãã㯠HTML Component ã® getter è¨å®ã使ã£ã¦ã¾ããã¤ã¾ããhtc ã§
<public:property name="length" get="getLength" /> <public:property name="remainingSpace" get="getRemainingSpace" />
ã¨è¨å®ãã¦ããããã«ãããããã£ã®åå¾ã®æã«æå®ããã¡ã½ãããèµ°ãããã«ãªã£ã¦ã¾ããå®æ 㯠length ã ã£ããã
function getLength() { var length = 0; for (var p in storage) length++; return length; }
ã¿ããã«ãªã£ã¦ã¾ãã
ã¾ã¨ã
ããã¾ã§èªãã§ãããããªãã¯ãã ãã¶ãããã§ãã
*1:ã¤ã¾ãattachEventã使ããªã