instance.data can be used to store information pertaining to a specific “instance” of an element on the page; but where does one store data that’s common/shared across all instances on a page?
For example, consider a plugin with a lot of constants that should be available to all instances, actions, etc. of a plug-in. Is there an officially sanctioned way to deal with this, or is it OK to just hang it off the
window object? I really don’t like polluting namespaces, so I’m wondering if there’s a better way to go about it.
Ya so instance.data is for the instance of the page. These are not shared across the different instance of the page and thus can’t share data to one another. your options are
https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage you can use that with an event liestener
Global object https://developer.mozilla.org/en-US/docs/Glossary/Global_object
Create a hidden DOM object with an id you can tap into
Thanks, @AliFarahat. It’d be nice if there was simply an object to which one could attach data to be shared across instances on a page - perhaps something like
context.class. Since the
context object is already available to action and state initialization functions, that would be ideal it seems. In fact, I might submit that as an idea.
BTW, I assume you mean “instance of the element on the page”, right?
Are you guys speaking Chinese?
Also in browser local storage, have you looked into that?
Thanks, @vini_brito, I appreciate the suggestion. I literally need just the “class” equivalent of the “instance.data” property (perhaps “context.shared_data”) - basically, an object to which to attach some key/value pairs (functions and data) and which would be accessible to each instance as well as across all plug-in functions (actions, initialization, etc.)
I was hoping for the simplicity and convenience of a JS object. I might use a global object in this case, but I’d prefer an object that’s properly scoped. Perhaps something like this suggestion will be implemented in the future.