I am just wondering how I can call a method of a service from within a function within a GSP. I tried the following but it does not seem to work:
<%@ page import="com.company.MyService" %>
<%
def myService =grailsApplication.classLoader.loadClass('com.company.MyService').newInstance()
%>
<html>
<head>
[...]
<script language="javascript">
function myFunction() {
if (${myservice.isSomethingAvailable()}) {
[...]
}
}
</script>
I am pretty new to javascript and Grails. Not sure how to achieve that or if it's even possible. Any help appreciated.
Thanks a lot
Jonas
loadClass().newInstance()
creates new instance of object, not spring bean (i mean it's not tied to grails infrastructure), i'm sure it's not what you wantrender(model: [myService: myService])
(you have to declare it at controller lever)render(model: [isSomethingAvailable: myService.isSomethingAvailable])
and test it as if ($(isSomethingAvailable)) {
<g:if test="${isSomethingAvailable}">
instead of preparing javascript to check value on client-side (because you already know the result)