简体   繁体   English

使用jQuery,AMD和require.js进行事件处理

[英]Event handling with jQuery, AMD and require.js

I am very new to AMD and require.js and have been struggling with a little problem for almost over a day. 我对AMD和require.js还是很陌生,并且几乎在一天之内一直在努力解决小问题。 I've tried different ways but not sure what is the correct way/right approach. 我尝试了不同的方法,但不确定什么是正确的方法/正确的方法。 I would appreciate some feedback from all the JavaScript gurus. 我希望所有JavaScript专家都提供一些反馈。

I am trying to implement an event handler for a text box that will listen for any input/changes. 我正在尝试为文本框实现一个事件处理程序,该事件处理程序将侦听任何输入/更改。 In the event handler, I would like to update a marker currently being shown on a map. 在事件处理程序中,我想更新当前在地图上显示的标记。 So, I defined two modules - open for openlayers and one containing my custom code for displaying the map, updating markers etc. 因此,我定义了两个模块-为openlayers打开,一个包含用于显示地图,更新标记等的自定义代码的模块。

The custom module looks like below: 自定义模块如下所示:

define('mymodule', [ 'open-layers', 'jquery', 'openstreetmaps','t5/core/console' ], function(
    openLayers, $,openStreetMaps ,console) {

    var init = function() {

    }
    var listenForChange = function(clientId) {
        clientId = clientId;
        var textBox = $(document.getElementById(clientId));
        console.debug(textBox);

        $('#addressLineTwo').on('change paste keypress input', function() {
            console.debug(textBox);
            console.debug('OnChange');
            console.debug($(this));
            console.debug($(textBox).val());
            openstreetmaps.clearMarkersAndShowAddress();
        });
    }
    return {
        init: init,
        listenForChange: listenForChange
    };
});

The problem is, when the event handler gets called as a result of input in the text field, the openstreetmaps variable in function defined as handler for "on" method is undefined . 问题是,当事件处理程序作为文本字段中输入的结果而被调用时,函数中定义为“ on”方法的处理程序的openstreetmaps变量undefined

They only way I could get around this was to change that line to something like this: 我唯一能解决这个问题的方法是将那条线更改为以下内容:

  require(['openstreetmaps'], function(openstreetmaps) { openstreetmaps.clearMarkersAndShowAddress(newAddress);});

Also, if I use such a construct how do I manage variables and passing data between all those closures? 另外,如果使用这种构造,如何管理变量并在所有这些闭包之间传递数据? It seems like that the inner closure cannot access variables from the outer closures or functions. 似乎内部闭包无法从外部闭包或函数访问变量。

I would really appreciate your help and feedback. 非常感谢您的帮助和反馈。

The variables from your other modules are accessible from within the event handler, you just need to change the define function parameter of openStreetMaps to openstreetmaps . 您可以从事件处理程序中访问其他模块中的变量,只需将openstreetmapsdefine函数参数更改为openStreetMaps即可。 That's why the extra require call works; 这就是为什么额外的require呼叫起作用的原因; you just need to match up the variable naming with your required module. 您只需要将变量命名与所需的模块匹配即可。

For managing variables, just expose everything public by returning them from the module, and those public variables can access whatever is defined in the module which creates a closure. 对于管理变量,只需通过从模块返回它们来公开所有公共内容,这些公共变量就可以访问在创建闭包的模块中定义的任何内容。 And each module only gets registered once so you can retain whatever state you want in each module. 每个模块仅注册一次,因此您可以在每个模块中保留所需的任何状态。

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

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