从其他JS文件访问函数 - DOJO

时间:2015-12-18 18:40:26

标签: javascript html dojo

我试图从另一个js文件中调用一个函数,该文件位于与我的主js脚本和html文件相同的应用程序目录中。我收到一个错误声称引用的函数不存在。我已经按照正确的顺序引用了主html文件中的两个脚本,但是我不知道为什么它无法检测到这个函数。我只能假设它与dojo如何解析文件并尝试了dojo / domReady有关!和dojo / ready模块希望强制加载脚本。我觉得这应该比我做到的要简单得多。任何帮助将不胜感激。

html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Asset View</title>
        <meta charset="utf-8">
        <link rel="stylesheet" href="http://js.arcgis.com/3.14/dijit/themes/claro/claro.css">
        <link rel="stylesheet" href="http://js.arcgis.com/3.14/esri/css/esri.css">
        <link rel="stylesheet" href= "styles/styles.css">
        <script src="http://js.arcgis.com/3.14/"></script>
        <script src="scripts/requests-js.js"></script>
        <script src="scripts/layout-js.js"></script>
        <script src="scripts/xmain-js.js"></script>
    </head>
    <body class = "claro">

    </body>
</html>

请求-js.js

require(["dojo/dom","dojo/on","dojo/request","dojo/json","dojo/parser","dojo/ready"


], function(dom,on,request,JSON,parser,ready) {

ready(function() {
    console.log("request start");

    function sendRequest (url,assetCode,fromDate,toDate) {
        console.log("Request innit");
        request(url,{
            method: "POST",
            handleAs: "json",
            headers: {"Content-Type": "application/json"},
            data: JSON.stringify(
                [
                    {
                        "AssetCodes": assetCode,
                        "FromGasDay": fromDate,
                        "ToGasDate": toDate

                    }

                ]
            )


        }).then(function(resp){
            console.log(JSON.stringify(resp));
            var naStorageJSON = resp;
        });
    }

    console.log("request end");

});

});

主js.js

require([
"dojo/dom","dojo/on","dojo/parser","dojo/json","dojo/request","dojo/ready"

], function(dom,on,parser,JSON,request,ready) {


ready(function() {



    console.log("Main Start");
    var url = "http://*********";
    var assetCode = ["**"];
    var toDate = "****";
    var fromDate ="*****";

    on(dom.byId("senecaLakeBtn"),"click",     sendRequest(url,assetCode,toDate,fromDate));







    console.log("Main End");


});
});

1 个答案:

答案 0 :(得分:0)

问题在于范围。通过在js脚本的顶部声明一个空数组(在其连续函数之外),该函数可以被应用程序中的其他脚本访问。

var app = [];
require(["dojo/dom","dojo/on","dojo/request","dojo/json","dojo/parser","dojo/ready"

], function(dom,on,request,JSON,parser,ready) {

ready(function(){
  app.sendRequest = function sendRequest (url,assetCode,fromDate,toDate) {
...
...
}
}

可以通过引用

从其他脚本调用它们

app.sendRequest(arg1,arg2,arg3);