What is the best way to pass arguments to a function on some object in a ReactiveHTML?
Lets say I have some object SomeJSObject
which has an increment
object which takes some argument and I want to call this from python.
Initially I was doing something like this:
class MyReactiveHTML(ReactiveHTML):
_template = ...
_add = param.Number(doc="Value to add")
_scripts = {
'render': """state.MyInstance = new SomeJSObject()""",
'_add': """state.MyInstance.increment(data._add)"""
}
def increment(self, number):
self._add = number
myhtml = MyReactiveHTML()
myhtml.increment(10) # Should work fine
myhtml.increment(5) # Also works
myhtml.increment(5) # Doenst trigger as the value is the same
Which works mostly but has the disadvantage that you cannot use the same value twice.
So I was thinking of doing something like this:
class MyReactiveHTML(ReactiveHTML):
_template = ...
_add = param.Event(doc="Triggers increment function")
_payload = param.Dict(doc="Dictionary of function arguments")
_scripts = {
'render': """state.MyInstance = new SomeJSObject()""",
'_add': """state.MyInstance.increment(data._payload[add])"""
}
def increment(self, number):
self._payload = {'add': number}
self._add = True
Such that the script is always triggered. Is this the way or is there a better way?
EDIT: Currently I also cannot get the second method to trigger the ‘_add’ script more than once. I’ve changed it to:
def increment(self, number):
self._payload = {'add': number}
self._add = not self._add
Which does work. Is it expected that Event
can only trigger scripts once or is this a bug?