Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:45:28+00:00 2026-05-26T16:45:28+00:00

JavaScript plays an increasingly important role in most web solutions, but I find that

  • 0

JavaScript plays an increasingly important role in most web solutions, but I find that it is much harder to decouple the JS code from the view specifics than it is for the server-side code.

What techniques are people using to decouple the JS code, in order to reduce the maintenance burden and to make it as resilient to minor view changes as possible?

To provide a concrete example, I have a view that looks sort of like the following:

<div id="signin" class="auth">
    <h2>Sign in</h2>
    <div id="resultbox" class="box" style="display: none;"></div>

    <div class="form">
        <p>Required fields are marked <span class="yellow">*</span></p>
        <form action="@Url.Action( MVC.Account.Authenticate() )" method="post" id="authform">
            <label for="Identification">Alias or email <span class="yellow">*</span></label></p>
            @Html.TextBoxFor( m => m.Identification, new { size="22", tabindex="1" } )

            <label for="Password">Password <span class="yellow">*</span></label></p>
            @Html.PasswordFor( m => m.Password, new { size="22", tabindex="2" } )

            <a href="#" class="but_styled" id="btn_signin" tabindex="3">Sign in</a>
        </form>
    </div>
</div>

The JS part is split into two files – the first is my “business logic” class and the second is used mostly for wiring:

(function (pp, $, undefined) {
    pp.account = {};

    function hideResultBox() {
        var box = $('#resultbox');
        box.hide();
        box.removeClass('info_box').removeClass('error_box');
    }
    function showResultBox(result) {
        var box = $('#resultbox');
        if (result.Success === true) {
            $('#resultbox_content').html(result.Message);
            box.addClass('info_box').show();
        } else {
            $('#resultbox_content').html(result.Message);
            box.addClass('error_box').show();
            var messages = '';
            for (var error in result.Errors) {
                messages += '<li>' + result.Errors[error] + '</li>';
            }
            if (messages !== '')
                $('#resultbox_content').append('<br /><ul>' + messages + '</ul>');
        }
    }

    pp.account.authenticate = function (data) {
        hideResultBox();
        $.post('/account/authenticate', data, function (result) {
            showResultBox(result);
            if (result.Success === true) {
                setTimeout(function () { window.location = result.Url; }, 1000);
            }
        });
    };
})(window.pressplay = window.pressplay || {}, jQuery);

And the wiring part:

$(document).ready(function () {
    $('#signin a').live('click', function () {
        var form = $(this).closest('form').serialize();
        pressplay.account.authenticate(form);
        return false;
    });
});

The problem with the code above is how closely it is tied to how the view looks (element ids that must be present, structure, etc.), but I have no good ideas on how it could be improved.

It just seems to me that, if I continue down this path, the JS files end up being a mess of properly encapsulated logic combined with all sorts of view-specific stuff.

Is this the best I can hope to do or are there any techniques I can apply to avoid some of this mess?

My own ideas on how to improve this revolve around building some sort of server-side generated “element selectors” JS class, such that I could write the JS without using quite as many string references to classes and element ids. But I am not sure how one would go about generating that or whether it’ll just be worse to maintain in the end.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:45:29+00:00Added an answer on May 26, 2026 at 4:45 pm

    Some thoughts:

    • Don’t base functionality on element “id” values, except for view features that really do involve elements that are “naturally” unique. Use classes instead whenever possible.
    • Use “data-” attributes to communicate feature configuration information from HTML to JavaScript. (But don’t put code in your “data-” attributes; in my opinion that’s a really terrible idea. Sorry about that, fans of Knockout. To each his own.)
    • Use an event pub/sub system to communicate between feature “bundles” to keep interdependencies under control.
    • Use <body> classes to characterize different sorts of pages in order to increase the efficiency of your “wiring”. That way, feature code can tell very quickly whether it needs to be even considered for a given page.

    edit — to clarify the last one: let’s say you’ve got some features that have to do with <form> pages, like date pickers, or auto-complete inputs, or other things like that. Sometimes there are features that only make sense for certain kinds of forms. In such cases, using a body class can make it simple to figure out whether a feature should bother to look around the DOM for elements to affect:

    if ($('body').is('.password-form')) { 
      // perform specific initializations
    }
    

    The web application I’m currently involved with isn’t too big, but it’s not trivial either. I find that I can keep just one big collection of JavaScript for the entire site and include it identically on every page, and I don’t have any performance concerns (currently) (well IE7 is slow, but this technique is not the cause of that).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Javascript I have code that will hide various sections in a MS CRM form
I've noticed that javascript alert() box plays the system sound exclamation found in Control
I am updating a DOM object on a javascript front-end that plays youtube video
I am trying to find a working jQuery image preloader that plays well with
Anyone clear on how big a role Javascript will play when HTML 5 comes
I cannot seem to debug my JavaScript code with Firebug. The play button is
Javascript: The Good Parts is a great book. Often I find myself reading passages
Javascript is pretty shaky for me, and I can't seem to find the answer
Video tags like below plays fine with iPhone, but not Android: <video id=video width=320
I have a website www.askvioletnow.com. I added a flash mp3 player that plays a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.