Custom Data Test

I've been working on the next Hype video and I realize that I probably shouldn't be teaching people to use window.data as that's not considered ideal programming. So, what's the alternative? That's when I looked at hypeDocument.customData.

The reason I use window.data is the reason not to use it — any JavaScript code can access it.

At first I was like, whatever, name your variables better. I haven't had a problem with collisions. Then again, I wasn't trying to put two quizzes on the same page. So, I tested it out. Yup, that's a problem. If both quizzes are using window.data then there's going to be a problem.

However, I wasn't sure if hypeDocument.customData would solve the problem. Wouldn't that have the same issue? So, I tested it. Apparently not!

Those are two Hype documents. On startup, they both write a hypeDocument.customData.color value. One writes "red" and the other writes "blue". When the button is pressed, the name of the button is changed to the hypeDocument.customData.color value.

Surprisingly, this worked. And just to be sure, I tested it with window.color — that didn't work.

Interesting. :thinking:
hypeDocument.customData is unique to the Hype document.

Custom Data Test.zip (93.8 KB)


Side Note: Perhaps the styling for code elements on this site should be changed. It's not easy to see the highlighting for code elements.

2 Likes

hypeDocument is unique to every document. It is constructed by the document loader by initing the runtime and then cached in HYPE.documents. That is how every extension can keep state at document level if intended, if you store stuff in hypeDocument or the later introduced hypeDocument.customData (since Hype build 612, it originated in the Hype StateKit thread). It is the "official" place for data as using hypeDocument directly is possible but not sanctioned.

2 Likes