在HTML中调用的Dojo函数

Dojo function called in HTML

本文关键字:Dojo 函数 调用 HTML      更新时间:2023-12-17

my html:

<html>
     <head>
          <script src="dojo/dojo.js"></script>
          <script src="app/my.js"></script>
          <script>
                handleResult(<%server response data%>);
          </script>
     </head>
     <body>
          <div id="data"></div>
     </body>
</html>

我的js:

require(["dojo/_base/Array"],function(Array){
     handleResult = function(data){
         Array.forEach(data,function(item,index){
               //Process the data
         });
     }
});

当页面加载调用handleResult时,我得到一个错误:

Uncaught ReferenceError: test is not defined 

但是我可以在firebug中得到这个函数window.handleResult请帮帮我。谢谢。

请不要使用onclick。
require(["dojo/on", "dojo/dom"],function(dom) {
     var button = dom.byId("demo"));
     on(button, "click", function(evnt) {
         console.log(evnt);
     })
});

require函数是异步的。你不能从require回调内部定义全局变量,然后尝试立即访问它。你也不应该从一开始就定义全局,这是AMD的基本原则之一。您的应用程序体系结构是错误的,永远不会工作。一旦应用程序加载到客户端,您需要使用AJAX调用来请求"服务器响应数据",而不是尝试执行您正在执行的操作。