2011-05-25 36 views
0

我在dojo对话框中使用闭包来防止函数和变量发生冲突。javascript,dojo,在jsp中使用外壳构建对话框

我有一个包含复杂对话框的jsp页面。它主要是在工作。只是一个非常恼人的细节。如果小部件发生变化,我该如何通知处理程序方法?我们希望尽可能多的对话框的java脚本保存在闭包中。

由于弹出窗口出现在多个位置,我希望未来的程序员能够用任何东西替换myVar。有没有一种方法来确定正确放什么的onChange

------ JSP页面-----
var myVar = new MyNewPopupStuff(42);
myVar.contstrucDialog();
......
includeTemplate template="myDialog.jsp"
.....

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

.....

< div id="myDialogData" dojotype="dijit.layout.ContentPane" region="right" splitter="true" class="data" style=" width:50%; " > <\/div>
....

-------- my.js文件----

function MyNewPopupStuff(param){ 

    this.contstrucDialog = function() 
    ....... 
    while(more widgets to add){ 
      fieldHolder=dojo.byId("myDialogData" 
    addWidget(widgetName,myDialogData); 
    } 

    addWidget = function(fieldHolder, widgetName){ 
    // need to print the name of the variable in "myVar.bar()" (myVar or what ever it may be) 
    dojo.place(newLabel, fieldHolder); 
     var newField = new dijit.form.ValidationTextBox({ 
     id: "i"+contactTypeId, 
     name: widgetName, 
     value: value, 
     trim: true, 
     **onChange:**??????when the widget is mofied I want to call the below modifyOnChange not some other modify on change I think I want something like myVar.modifyOnChange???? 

    }); 
    } 


    this.modifyOnChange = function() 
    { 
     alert("modified"); 
    } 
} 

回答

0

如果你使用了一个框架,实现了功能的EcmaScript 5绑定方法,使用方法:

onChange: modifyOnChange.bind(this), 

别人自己有实现它

onChange: (function(self) { 
    return function() { 
     return modifyOnChange.apply(self, arguments); 
    } 
})(this), 

myVar.modifyOnChange可能会工作,但如果您将代码绑定到您期望持有该对象的全局代码,那么将这些东西抽象到类中有什么意义呢?如果有人想添加另一个MyNewPopupStuff对象会发生什么?

+0

谢谢你的帮助。 – 2011-05-27 18:01:59

+0

不客气。 – LHMathies 2011-05-27 19:36:52