简体   繁体   English

用jquery事件更新javascript中的主类的正确方法是什么?

[英]What is the proper way to update main class in javascript with jquery events?

Assuming I have something like: 假设我有类似的东西:

var MyApp = function() {
this.name = "Stacy"
}

MyApp.prototype.updateName = function(newname) {
this.name = newname;
}

In my main page I have a : 在我的主页上,我有一个:

$(function () {
  var instance = new MyApp();
})

I have a button event handler that would update the name: 我有一个按钮事件处理程序,它将更新名称:

$("#button").on("click", function(evt) {                        
    // Update the name in MyApp to something else...
    instance.name = "john" // I do not like using instance here, because it has to be "instance" has to be created before I can use it. I want to be able to make this independent of "instance" being created or not
});

What is the proper way to do it such that the button handler would update "MyApp" to have the correct name, without explicitly using the created "instance" of myapp as part of the button's click handler? 这样做的正确方法是什么,以便按钮处理程序可以将“ MyApp”更新为正确的名称,而无需将创建的myapp的“实例”显式用作按钮单击处理程序的一部分?

ideally I would like to shove that jquery event handler somewhere into "MyApp" such that I could do something like: 理想情况下,我想将该jQuery事件处理程序推送到“ MyApp”中的某处,以便可以执行以下操作:

MyApp.prototype.events = function() {
    $("#button").on("click", function(evt) {                        
        this.name = "john"
    });
}

Though it doesnt work because this refers to something else. 虽然它不起作用,因为它涉及其他内容。

How to properly structure my application such that the event handler is more or less updating the properties of the "MyApp" so that it can be independent of the created "instance" (ie i no longer have to use the "instance.")? 如何正确构造我的应用程序,以便事件处理程序或多或少地更新“ MyApp”的属性,以便它可以独立于所创建的“实例”(即,我不再必须使用“实例”。)?

First, if you create an setter function, it's a good idea to use it !! 首先,如果创建一个setter函数,则最好使用它! :D :D

$("#button").on("click", function(evt) {                        
    // Update the name in MyApp to something else...
    //instance.name = "john"
    instance.updateName("john");
});

And then, it does not make sense to do put an event handler inside of a method of your object MyApp, since it will never bind the onclick event until you fire events() 然后,将事件处理程序放置在对象MyApp的方法中是没有意义的,因为在您触发event()之前,它永远不会绑定onclick事件。

Then... my way to organize this , is to use the jQuery document onload to bind all the DOM objects with the function of your applications. 然后... 我组织此方法的方法是使用jQuery文档onload将所有DOM对象与应用程序的功能绑定在一起。 Usually something like this: 通常是这样的:

MYAPP = {};

MYAPP.say_something = function () {

    alert('lol, you clicked me!');

};

...

$(function () {

    $('#my_button').click(MYAPP.say_something);
    $('#other_element').mouseenter(MYAPP.another_method);

});

And for big applications, where you have to work with a lot of elements, you can organize your code much better if you have a namespace for your DOM elements, something like this: 对于大型应用程序,您必须处理许多元素,如果您有DOM元素的命名空间,则可以更好地组织代码,如下所示:

MYAPP.fetch_dom = function () {

    return {
        my_button: $('#my_button'),
        other_element: $('#other_element')
    };
};

And you can bind the events in a very neat way 您可以以非常简洁的方式绑定事件

$(function () {

    // first initiate DOM
    my_dom = MYAPP.fetch_dom();

    // Then bind events
    my_dom.my_button.click(MYAPP.say_something);
    my_dom.other_element.mouseenter(MYAPP.another_method);

});

This way you don't have to look for the specific elements in the DOM from a thousand points of your programme, spreading hardcoded id's everywhere and performing noneffective searches against the DOM structure. 这样,您不必从程序的一千个点中查找DOM中的特定元素,而将硬编码ID散布到各处,并且无需对DOM结构执行无效搜索。

Finally, it is much better to use literals in JS rather than using the word new. 最后,在JS中使用文字比使用new这个词要好得多。 JS is a prototypical OOP language and new is a little bit "against nature" that can be a pain in the ass. JS是一种典型的OOP语言,而new则是“反对自然”,可能会让人感到痛苦。

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

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