0

Call JavaScript function from ViewModel

asked 2016-06-14 07:32:53 +0800

kleponboy gravatar image kleponboy flag of Indonesia
54 5
http://stupidprogrammers....

Hi,

I want to call JavaScript function via init method in the ViewModel, It always throw an error "onLoad is not defined".

my jsTest.js :

 function onLoad(){
   alert("Hello");
 }

my test.zul :

<div apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('com.test.TestViewModel'>

  <script type="text/javascript" src="jsTest.js"></script>

</div>

and myViewModel :

@Init
public void init(){
  Clients.evalJavaScript("onLoad()");//Throws an error

}

but if I try called the function via button onClick event it will run just fine.

@command
public void callFunction(){
   Clients.evalJavaScript("onLoad()");//No error
}

Pls Help,

Regards, Klepon

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-06-14 08:50:37 +0800

gganassin gravatar image gganassin flag of Luxembourg
540 6
http://www.hybris.com/

Hello!

1 - i'd put also the ; char inside the Clients.evalJavaScript("onLoad();"); 2 - Try to move the <script> tag before the

as i think it is still not loaded when the @init is triggered (binder calls the method with this annotation when initializing a ViewModel)

Giovanni

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2016-06-14 07:32:53 +0800

Seen: 43 times

Last updated: Jun 14 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More