0
votes

Using Struts2 with tiles and dojo something released in last 2 years. trying to move gobal code for button onclick and form validation into a closure.

------myjsp.jsp page-----
function doDialog(){
var myVar = new MyNewDialog(42);
myVar.showDialog();
}
......
< button ... to show dialog.. onclick="doDialog()" ..../>
< tiles:insertTemplate template="myDialog.jsp"

.....

--------myDialog.jsp----
< script type="text/javascript" src="my.js"/>

....

< div dojoType="dijit.form.Button" iconClass="toolbarIcon toolbarIconSave" id="mySaveButtonId" showLabel="true" onclick="mySaveButtonPressed();">

< /div>

--------my.js file----
// This function works because it is global, I want to move it into the closure.
function mySaveButtonPressed(){
.....
validateDialog();
...
}
function validateDialog(){
.. yes it calls the the struts2 validators... but also does some other things.
}

function MyNewDialog(param){

     this.contstrucDialog = function(){
      ...
     }

     //desired location saving code
     //this.mySaveButtonPressed = function(){
     //.....<
     //validateDialog();
     //...
     //}
     //function validateDialog(){
     //  .....
     // }

    ..... everything but the method for validating and saving the dialog data....
}


I moved the save and validate methods into the closure, and tried add a connection to my target method. the mySaveButtonPressed is never executed. I thought the whole point of connect was to connect a widget event to some method call.
myjsp.jsp

var myVar = new MyNewPopupStuff(42); var mySaveButton = dojo.byId("mySaveBut \tonId");
dojo.connect(mySaveButton,"onclick", myVar.mySaveButtonPressed());
myVar.contstrucDialog();

Since I really don't want to putting a lot of "myVar" stuff anywhere, I tried "this" instead of myVar. no luck.

QUESTION: How should I move this code into a closure????

--------------------------Had a thought but cannot test it at moment------

Perhaps the dojo.connect should go into the closure where I construct the dialog contents.

QUESTION: What is the industry best practice for handling this common delema?

1

1 Answers

0
votes

in my java script that built the dialog I put the javascript that built my dialog.

works

function MyNewDialog(param){

 this.contstrucDialog = function(){
  ...
  dojo.connect(saveButton,"onclick",this,mySaveButtonPressed,true );
   ......
 }

but does not like

dojo.connect(saveButton,"onclick",this,mySaveButtonPressed(),true )

So minor question that someone may be able to answer for me. When do I put parens when do I not? I am calling a function mySaveButtonPressed(), Why would'nt I use "mySaveButtonPressed()"?