简体   繁体   English

面向对象的Javascript自定义addEvent方法

[英]Object-Oriented Javascript custom addEvent method

I have a question regarding developing object-orientated javascript and parsing variables. 我有一个关于开发面向对象的javascript和解析变量的问题。 Please see this blog by net.tutsplus - The Basics of Object-Oriented JavaScript for more info. 通过net.tutsplus-面向对象的JavaScript基础了解此博客

I have the following code which creates an event: 我有以下创建事件的代码:

$(document).ready(function() {
    tools.addEvent('.someButton', 'click', user.getUserDetails);
)};

var tools = {
    addEvent: function(to, type, fn) {
        $(to).live(type, fn);
    }
}

var user = {
    getUserDetails: function(userID) {
        console.log(userID);
    }
}

As you can see, it calls the addEvent method with three variables; 如您所见,它使用三个变量调用addEvent方法。 the DOM element to attach the event to, the type of the event and the function to run when the event is triggered. 附加事件的DOM元素,事件的类型以及触发事件时要运行的函数。

The issue I am having is parsing a variable to the getUserDetails method and I know of 2 options: 我遇到的问题是将变量解析为getUserDetails方法,我知道2个选项:

  1. I could obviously have 1 line of code at the start which could check an attribute of the sender. 很明显,我可能在开始时有1行代码,可以检查发送者的属性。 For example, the .someButton could have an attribute userID="12345" . 例如, .someButton可以具有属性userID="12345" However, this is not ideal because the function is run from several different places - meaning this check is not always available (and the code is harder to manage). 但是,这并不理想,因为该功能是从多个不同的地方运行的-这意味着此检查并不总是可用的(并且代码更难管理)。

  2. A better option could be to have another method like user.willGetUserDetails and use this method to get the attribute userID from the DOM. 更好的选择可能是拥有另一个方法,如user.willGetUserDetails并使用此方法从DOM获取属性userID This could be run from anywhere on the page, and would call getUserDetails after getting the userID . 这可以在页面上的任何位置运行,并在获取userID之后调用getUserDetails Whenever the user details comes from within another function, we would simply call getUserDetails directly. 每当用户详细信息来自另一个函数时,我们都将直接直接调用getUserDetails

  3. What would be ideal, is if I could amend the code above to pass a variable directly - even an undefined one. 理想的情况是,如果我可以修改上面的代码以直接传递一个变量-甚至是未定义的变量。 Does anyone know how this could be achieved? 有谁知道这将如何实现?

Add one more argument to your addEvent code that accepts data to pass to the event. 在您的addEvent代码中再添加一个参数,该参数接受数据以传递给事件。

var tools = {
    addEvent: function(to, type, data, fn) {
        if ($.isFunction(data)) {
            fn = data;
            data = {};
        }
        $(to).live(type, data, fn);
    }
}

Also, i'd suggest using delegate instead, or .on in 1.7+ 另外,我建议改用委托,或1.7+中的.on

var tools = {
    addEvent: function(to, type, data, fn) {
        if ($.isFunction(data)) {
            fn = data;
            data = {};
        }
        $(document).delegate(to, type, data, fn);
    }
}

or 要么

var tools = {
    addEvent: function(to, type, data, fn) {
        if ($.isFunction(data)) {
            fn = data;
            data = {};
        }
        $(document).on(type, to, data, fn);
    }
}

Now you can use it like this: 现在您可以像这样使用它:

$(document).ready(function() {
    tools.addEvent('.someButton', 'click', {userID: theuserid}, user.getUserDetails);
)};

var user = {
    getUserDetails: function(event) {
        console.log(event.data.userID);
    }
}

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

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