I have a Label and a Button in Perspective. When the button is clicked, I want to dynamically add a new value change script to the Label's text
property. Specifically, the number of lines I write in the button's script should be added to the value change script of the Label. Has anyone implemented this kind of functionality before? If so, please share your solution or guidance
What is the real problem you are trying to solve. What you are proposing is strange and there must be a better way to do it.
When I click the button, I want the script system.perspective.sendMessage('test', {})
to be injected into the Label component's onValueChange
script. Is this possible?
I'm asking because I was able to inject script lines into a tag's value change event, so I'm wondering if a similar approach is possible for Perspective components.
While, it may be possible through some fancy manipulation of the view files JSON on the Gateway, there would be a large delay, 5 secs by default IIRC.
The real question is why? Why do you need to inject this function into a script? Just put it there to begin with.
You couldn't pay me enough to implement the ability of an end user to "add arbitrary code" into a view.
Also, in python functions are objects, and they can be passed around just like other objects you may be more familiar with.
I didn't fully understand your question. What I'm trying to ask is whether this functionality is currently possible, as it is essential for my development requirements. That’s why I’m checking if this feature is officially supported. If anyone knows of an alternative approach or has any relevant resources, please share the links or provide guidance. Any support on this would be greatly appreciated.
Currently possible and officially supported are not mutually exclusive.
What I, and I believe @Transistor are trying to understand is why this is "essential". What functionality is this empowering?
Very difficult for us to suggest alternative approaches when we don't know what it is you're really trying to achieve. And no, injecting active code into an event script at runtime, is not what you're trying to achieve, that is the means you are attempting to get to some end.
What is the end result that you want to achieve?
I can't imagine any case where this is necessary where you couldn't solve this by having onValueChange script read something like
for message in system.tag.readBlocking(tagpath):
system.perspective.sendMessage(message, {})
and then the onClick just appends to the tagpath
I wouldn't involve tags just yet.
@ARIFRAHUMAN_A : What @lrose and @Transistor are hinting at is this: https://xyproblem.info/
They're saying you're looking for a way to do what you think is the solution to your problem, instead of asking how to solve your problem.
Tell us what this would solve, and we might find a better way to solve it.
Fair
At the end of the process, I am using an Accordion component where the items
data is passed dynamically. Each item contains an expanded
property, and I want to inject a value change script into that property. The goal is to inject the script at the time the items
data is assigned to the Accordion. Specifically, I want the script to execute and call a message handler (e.g., system.perspective.sendMessage
) when an item’s expand icon is clicked (i.e., when the expanded
property changes). This is the functionality I’m trying to achieve.
Add a message_handler
parameter to your repeated view, and use that parameter as the argument to system.perspective.sendMessage
Hi @nminchin,
Do you have any idea or suggestions regarding this requirement? I’m trying to dynamically inject a value change script into the expanded
property of an Accordion component item. it will very helpful for me
Dont inject the script, just make its parameters dynamic.
Or is there a reason this wouldn't be enough ?
I see no need to inject a script.
Just place the script on the view that is being repeated in the Accordion. Better yet, place the actual script in a project library, and call out to that script from the repeated view. Change the parameters you send to the function.
You have still yet to say why you feel you need to inject this script.
Also, please don't ping random users. If Nick felt like he had something to add, he would post a reply.
Add the message handler names and payloads to your items array.
On the Accordion component Event | onItemExpanded | Script use something like the following:
def runAction(self, event):
handler = self.props.items[event.index].messageHandlerName
if handler is None:
return
system.perspective.sendMessage(
handler,
self.props.items[event.index].messageHandlerPayload,
scope = "view"
)
yep, this works and is cleaner than the tag idea
Thanks, @Transistor That's a good idea — I'll try it.