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

  • SEARCH
  • Home
  • 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 7952193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:47:15+00:00 2026-06-04T02:47:15+00:00

I have a class for part of a GUI. It is a toggle switch

  • 0

I have a class for part of a GUI. It is a toggle switch that has a switch public member function that puts the GUI switch into whatever position you want. When you click on the GUI element, I would like an onToggle event fired along with the GUI calling the switch function.

Here are my choices (that I’m considering and want your advice on) to implement this:

1) Make a setOnclick public member function that my controller can use to assign the firing of the event to the element and take care of the switch function call.

toggleInstance.setOnclick(function() {
    onToggle.fire();
    toggleInstance.switch();
})

2) I can just put the event firing and switch function call inside the class itself

element.onclick = function() {
    onToggle.fire();
    public.switch();
}

I think it would be nice to have the GUI classes (such as my toggle switch) entirely devoid of any logic or functionality, but I’m not sure if this is necessary.

I’m not using an MVC framework, but I do like to apply the principles of separating functionality between MV and C.

Can you think of any potential downsides to either method in the future?

  • 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-06-04T02:47:16+00:00Added an answer on June 4, 2026 at 2:47 am

    HTML/CSS/JavaScript:

    In client side code, you have HTML that represents your content, CSS that defines your presentation of that content, and JavaScript, which you use to determine and assign the behavior of the content and presentational elements.

    In short, you have the right idea here. Just by keeping JavaScript out of your HTML and HTML out of your JavaScript, you’ve done a lot in terms of proper planning of your architecture.

    In the JavaScript, you can follow MVC, but for the most part, as long as you remember HTML is content, JavaScript is behavior, and CSS is presentation, you’ve done so much to make your code highly maintainable.

    With that said, let’s focus on your JavaScript.

    MVC:

    While I think you do have the right idea your first example function may not be named exactly correct. When I think of “setOnclick”, I think that you’re setting the onclick event, not firing the onclick event.

    Thus, I picture this:

    // reuse this same function to bind that same event to other similar buttons
    toggleInstance.setOnclick(function(element) {
    
        element.onclick = function() {
            onToggle.fire();
            public.switch();
        }
    });
    

    If you ever need more than one control to repeat that event, you can just simply pass in that element and bind the same click handler to it.

    You could take it a step further by defining the actual event handler in a separate function

    // event handler is now named, so it can be easily reused
    toggleSwitch: function() {
        onToggle.fire();
        public.switch();
    }
    
    toggleInstance.setOnclick(function(element) {
    
        // bind the event to the element passed in
        element.onclick = toggleInstance.toggleSwitch;
    });
    

    My only advice is to be careful not to over-architect. MVC is a balance, and something that’s over-architected can be just as unwieldy and difficult to work with as something that’s carelessly under-architected.

    It’s good that you’re thinking about these details; they will pay dividends in the form of lower technical debt if you know what events you’ll need to reuse and focus on those.

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

Sidebar

Related Questions

I have a class that builds my gui. One part of the gui is
I have a class, which is part of a code library project that was
If I have a .Net class that is not part of any namespace then
I have the following ugly if statement that is part of a class that
I have this part of the function : $jQ('.product-collateral .product-tabs li').each(function(index){ $jQ(this).attr('id', $jQ(this).find('h3').attr('class')); if(index
Here is the problem I'm running into - I have a GUI class implementing
I have a GUI C# application that has a single button Start/Stop. Originally this
I have an application that has a class named: UploadItem. The application creates uploading
i have a class which, when a certain part of the gui is clicked
I have a Class MyClass, that when it runs it initialize a GUI awt

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.