简体   繁体   English

有条件的执行JavaScript

[英]conditional execution of unobtrusive javascript

Working on an MVC application at the moment that has quite a large number of views and unobtrusive script to go along with it. 目前,在具有大量视图和不引人注目的脚本的MVC应用程序上工作。 Currently using jQuery. 当前使用jQuery。 All of the unobtrusive code is neatly organized in separate files/folders and then bundled/minified into a single file so I decided to do conditional checks to determine if chunks of unobtrusive code blocks should even run based on an element on the current page. 所有非干扰性代码都整齐地组织在单独的文件/文件夹中,然后捆绑/最小化为一个文件,因此我决定进行条件检查,以确定是否应基于当前页面上的元素运行非干扰性代码块。 I'm assuming, maybe incorrectly, that by doing these checks this will help lower the amount of time jQuery is spending on each page running unobtrusive wire-up code that isn't necessary based on the current view. 我假设,也许是错误地,通过执行这些检查,这将有助于减少jQuery在每个页面上花费的时间来运行基于当前视图的不必要的自动连接代码。

Is this a common practice to follow for unobtrusive javascript or is there a more common practice? 这是不打扰的javascript的通用做法,还是有更通用的做法?
Note: This is not SPA. 注意:这不是SPA。

$(function () {
    if ($(".flag-dialog").length > 0) { // check if this block is even necessary for the current page
        $(document).on("click", ".flag-dialog button.c", function () {
            ui.modalDialog.close(".flag-dialog");
            return false;
        });
        // more code...
    }
});
$(function () {
    if ($(".comment-section").length > 0) { // check if this block is even necessary for the current page
        $(document).on("click", ".comment-section button.s", function () {
            // ....
        });
        // more code...
    }
});

I don't think it's worth it. 我认为这不值得。 The expensive part is selecting the elements, which your code always does once in the if statement condition. 昂贵的部分是选择元素,您的代码在if语句条件下始终会执行一次。 Worse, it does this when the page loads, which is arguably the least ideal time to be doing a lot of heavy lifting. 更糟糕的是,它在页面加载时执行此操作,这可以说是进行大量繁重工作的最不理想的时间。

The actual binding and then dispatching of the click events, which is what your code tries to optimize out, is relatively cheap since your event handler will never actually be dispatched to, and the call to "on" doesn't immediately evaluate the selector expression, so it's actually a pretty cheap operation. 单击事件的实际绑定和调度(这是您的代码尝试优化的)相对便宜,因为您的事件处理程序永远不会真正调度到该事件,并且对“ on”的调用不会立即评估选择器表达式,因此实际上这是一个非常便宜的操作。

More importantly, your optimization results in more code, and all of the ongoing maintenance expenses that go along with those extra checks. 更重要的是,您的优化将导致更多的代码,以及与这些额外检查一起产生的所有日常维护费用。

That said, if you really want to do those extra checks, and if this is a pattern you think you'll use in a lot of places, instead of adding a bunch of if statements throughout your code, consider writing a small function that does it for you: 就是说,如果您真的想做那些额外的检查,并且如果您认为这是一种模式,那么您会在很多地方使用它,而不是在代码中添加一堆if语句,可以考虑编写一个小函数它为你:

function attachGlobalEvent(eventName, selector, handler)
{
    if ($(selector).length > 0)
        $(document).on(eventName, selector, handler);
}

I still think that this is going to be a bit slower than not having it in a bit too many cases, but at least it won't result in as much code. 我仍然认为这会比在太多情况下没有它慢一些,但至少不会导致太多代码。 Just be aware that your handler won't be called if the first matching element isn't added to the DOM until after the event-attaching code is executed, which is usually why you'd attach the event indirectly like this to begin with. 请注意,如果在执行事件附加代码之后才将第一个匹配元素添加到DOM,则不会调用您的处理程序,这通常就是为什么您要像这样间接地附加事件的原因。

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

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