简体   繁体   English

RequireJS并在模块中使用this.id?

[英]RequireJS and using this.id in a module?

I am switching an app over to RequireJS. 我将应用程序切换到RequireJS。

In my app, I detect when a user clicks on an element with a certain class on it. 在我的应用中,我检测到用户何时单击具有特定类的元素。

I use the click event and this.id to get the unique id of the element. 我使用click事件和this.id来获取元素的唯一ID。

Now that I've moved this function to a module for Require, I only get 'undefined' back for this.id. 现在,我已经将此功能移到Require的模块中,我只为this.id返回“未定义”。

I'm guessing the module doesn't have a reference for what was clicked when the function is called. 我猜该模块没有调用该函数时单击的引用。

I also can't seem to pass this.id to the module. 我似乎也无法将this.id传递给模块。

'the use of a keyword as an identifier is invalid'. “使用关键字作为标识符无效”。

So, how do I get the unique ID of the element clicked? 那么,如何获得所单击元素的唯一ID?

 $(document).on('click', '.userSelect', function (myID) {

    var retConvo = getConvoID.convoData();
    convoID = retConvo.convo;
    isGroup = retConvo.group;
    console.log("Conversation ID is: " + convoID + " and IsGroup: " + isGroup);
});

and here is the module code it's calling. 这是它正在调用的模块代码。

define(function() {

  return {
    convoData: function(myID) {
      var userSelected = this.id;
      console.log('User Selected ID is: ' + userSelected);
      if (userSelected == myID) {
        alert("You can't message yourself.");
        return false;
      } else {
        console.log("Selected User ID is: " + this.id);
        // change background to show user is selected.
        $("div.userSelected").removeClass("userSelected");
        $(this).addClass("userSelected");
        convoID = this.id;
        isGroup = 'N';
        console.log("emitting convo users id: " + convoID);
        return {
          convo: convoID,
          group: isGroup
        };
      }
    }
  }
});

So, when it get's to this.id above, it is undefined. 因此,当到达上面的this.id时,它是未定义的。 Worked fine when it was all in one js file. 当全部放在一个js文件中时,效果很好。

Thanks for any help and pointers. 感谢您的帮助和指点。

A couple of things: 有两件事:

  1. It doesn't seem like you're passing myID to the convoData function 它似乎并不像你传递myIDconvoData功能
  2. this in the function refers to the local this . this在功能指的是本地this If you want it refer to the clicked element, either pass the element as a parameter to the function, or bind it at function call time: var retConvo = getConvoID.convoData(myId).bind(this); 如果希望它引用单击的元素,则可以将该元素作为参数传递给函数,或者在函数调用时将其绑定: var retConvo = getConvoID.convoData(myId).bind(this); Alternatively, var retConvo = getConvoID.convoData(myId, this); 或者, var retConvo = getConvoID.convoData(myId, this); and then change convoData to function(myID, element) and refer to element.id ; 然后将convoData更改为function(myID, element) convoData function(myID, element)并引用element.id ;
  3. Use === instead of == in your code - has no bearing on this question per se, but will help you avoid problems down the line. 在您的代码中使用===代替== -本身与这个问题无关,但可以帮助您避免出现问题。

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

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