Is it possible, via client-side APIs, to get the content of a Script Component...and then make changes to it?
I have a script component on a page and want to load the content from it, replace some text, then save those changes.
Ideally, I could then persist those changes and see them the next time i visited that page.
No. And that's a good thing otherwise users would be able to inject arbitrary code that would be saved and then executed by other users thus opening a security hole.
...or a way to programatically get the ID of the script component that a function is executing in.
Either or :)
No. And that's a good thing otherwise users would be able to inject arbitrary code that would be saved and then executed by other users thus opening a security hole.
And to answer the question in second post, you can use document.currentScript
developer.mozilla.org/.../currentScript
Hope this helps, Thierry.
What I meant was the Rollbase Script Component.
I'm trying to find a way to persist some state data for a particular Script Component. Here's an example:
There's a button. When you click the button, a modal dialog pops up. The dialog has a text box where you can enter some text. When you click "save" on the dialog, the value of that text box gets persisted.
The next time the script runs, it can access that data by querying for it. (Using something like the "original id" of the Script Component as the key)
The other piece of the puzzle is deleting the state data when a script component is removed from the page. Easy if the components could be edited via the client-side api...tougher if persisted elsewhere because there are no events that fire when a component is deleted :(
Maybe one way to solve this would be to have your scripts and the associated states stored in an object?
developer.mozilla.org/.../sessionStorage
Have you explored browser's local/session storage? Your script component can persist data in component specific keys and retrieve them on demand. Note: to store complex data you can use json objects and then JSON.stringify() them to be stored in the storage.
If you were to use session storage, the state wouldn't be shared to other users (not sure if it is a requirement of yours) and it wouldn't be shared if user logins from other device or other browser.
Thierry.
I appreciate all the input.
Storing the info in another object was a good way to go. Now I can grab the same info (at runtime, and for any user) and use it to control rendering of the component.
Will post a demo of the technique and link it in this thread soon. :)
Here's the thread with the demo link.