I have a form (loaded using HashRouting) where I’ve embedded a component (custom html form).
That component has a javascript function that I want to call from Python.
When I tried to call the javascript from the Component’s show_form event, I keep running into
“could not find global js function”
I’ve tried both self.call_js() and anvil.js.call_js()
When I moved my code to the form that embeds the component, I get the following:
AttributeError: ‘Route’ object has no attribute ‘call_js’
I have one other area where I have been able to successfully call a Javascript function on the standard_page.html template using anvil.js.call_js() function but I want to contain this piece of Javascript closer to the form, rather than moving in out to the main template.
I can’t see that it will be because of hash routing…
the can’t find the global js function Error is typically because either the form is not yet on the screen so it’s JavaScript has not been loaded yet. (But using the form show event typically solves this)
Or
You have a syntax error in the JavaScript and so it is not accessible. It happens to me all the time when I write JavaScript in the anvil ide. If I can’t spot the error I copy the code into a code editor with JavaScript linting which normally fixes it.
edit: @phil’s post was before hashrouting existed.
Have you tried loading just the component to see if it works on its own? Either by making it the startup form, or adding it as a component to a form that is the startup form
To provide more context, the reason why I’m doing this is because:
calling self.call_js() from the Component itself throws “cant’ find global js function” error
I moved the call to the Form that contains the component as it controls the display (visible) of the component based on business rules. So only when visible=true on the component, does the javascript get called. However the error here is “AttributeError: ‘Route’ object has no attribute ‘call_js’”
Are you sure that you’ve correctly set the componentsform show event? This is likely the problem with your option 1.
(edit - or a syntax error see earlier suggestion)
Blank panel forms don’t have a call_js method (only HTML Forms do) which is why option 2 fails. You could do self.component.call_js if you really want to call the js method of the component from within the blank panel.