简体   繁体   English

在jQuery click事件中,调用存储在单独的dojo js文件中的函数

[英]In a jQuery click event, call a function stored in separate dojo js file

When trying to break up the JS code, I've pulled my jQuery bootstrap click events into their own file. 当试图分解JS代码时,我将jQuery引导点击事件拖入了自己的文件中。

Everything loads correctly but when trying to reference a function that is in my dojo file from the jQuery file, the end result is "zoomToStreams not defined()"; 一切都正确加载,但是当尝试从jQuery文件引用我的dojo文件中的函数时,最终结果是“ zoomToStreams not define()”;

Is there a proper way to break everything out into easily managed pieces for code-reusability and have the different libraries (jQuery, dojo) co-exist? 是否有适当的方法将所有内容分解为易于管理的部分,以实现代码可重用性,并使不同的库(jQuery,dojo)共存? Both are loaded via tags at the foot of my document. 两者都通过我的文档底部的标签加载。

// dojo.js

require(["dojo modules",...],function(dojo,...) {
    ...
    function zoomToStream(targetStream) {
       ...
    }
});

// jQuery.js

$(document).ready(function() {
    $("#streamSelection li").click(function(e) {
        switch (e.target.text) {
            case "All Streams":
                zoomToStream("all");
                break;
            case "First Stream";
                zoomToStream("1");
                break;
            ...
        }
    }
 });

Turn your first snippet into a module that you can then require for your jquery code. 将您的第一个代码片段转换成一个模块,然后您就可以使用该模块来编写jquery代码。

dojo.js (this isn't really what you named your js file is it?) dojo.js(这不是您真正命名的js文件吗?)

define("zoomToStream",["dojo modules",...], function(dojo,...) {
    ...
    function zoomToStream(targetStream) {
       ...
    }
    return zoomToStream;
});

jQuery.js (this isn't really what you named your js file is it?) jQuery.js(这不是您真正命名的js文件吗?)

require(["zoomToStream"],function(zoomToStream) {
    $(document).ready(function() {
        $("#streamSelection li").click(function(e) {
            switch (e.target.text) {
                case "All Streams":
                    zoomToStream("all");
                    break;
                case "First Stream";
                    zoomToStream("1");
                    break;
                ...
            }
        }
    });
});

modify to fit your file structure etc, kinda seems odd to use those filenames for your js files. 修改以适合您的文件结构等,将这些文件名用于js文件似乎有点奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM