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 8323903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:52:13+00:00 2026-06-08T23:52:13+00:00

I am using module pattern in JavaScript. I have a file say controller.js which

  • 0

I am using module pattern in JavaScript. I have a file say controller.js which handles events. The problem is I want to use sub-modules. But since the events are handled in the controller.js how will I handle the event in the sub-module if any arises?
I dispatch a custom event called PAGE_DISPLAYED from another file say view.js. This event is listened by the controller.js and depending on the displayed page, it does other things, like binding additional event handler specific to that particular page. How to do this using sub-modules.

Sample code:

window.KDJ || (window.KDJ = {});    //namespace
KDJ.controller = (function () {
    $(document).off('PAGE_DISPLAYED');
    $(document).on('PAGE_DISPLAYED', function (e, data) {
    switch(data.pageId) {
    case "page1":
        // do something..
        break;
    case "page2":
        // do something..
        break;
    }
    });

    return {
        // exported methods and props..
    };
})();

How can I extend the above code and the event delegation? Or is the code architecture posing any restrictions.

I am using jQuery Mobile and dispatching the PAGE_DISPLAYED event is done by listening to pagechange event:

window.KDJ || (window.KDJ = {});    //namespace
KDJ.view = (function () {  
    // ...
    $(document).off('pagechange');
    $(document).on('pagechange', function (e, ui) {
        //...
        $(document).trigger(PAGE_DISPLAYED, {'pageId': loadedPageId, 'key': key});
    });
})();

Thanks.

  • 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-08T23:52:14+00:00Added an answer on June 8, 2026 at 11:52 pm

    Ok, I tought a few possibilities but in terms of readability the one I like the most is:

    window.KDJ || (window.KDJ = {});    //namespace
    KDJ.controller = (function () {
        $(document).off('PAGE_DISPLAYED');
        $(document).on('PAGE_DISPLAYED', function (e, data) {
    
            //Use the Id if it exsits, if not, try to execute the function
            switch(data.pageId) {
                case "page1":
                    // do something..
                    break;
                case "page2":
                    // do something..
                    break;
                default: 
                    if(data.customFunction)
                        data.customFunction();
                    break;
            }
    
        });
    
        return {
            // exported methods and props..
        };
    })();
    
    
    KDJ.view = (function () {  
        // ...
        $(document).off('pagechange');
    
    
        $(document).on('pagechange', function (e, ui) {
    
            //the function will be executed
            $(document).trigger(PAGE_DISPLAYED, {
                customFunction = function(ui) {
                    console.log("I'm doing something awesome with the ui!");
                }
            });
    
        });
    
        $(document).on('pagechange', function (e, ui) {
    
            //here I'll use the switch
            $(document).trigger(PAGE_DISPLAYED,  {
                'pageId': loadedPageId, 
                'key': key
            });
    
        });
    })();
    

    Here you have another option, without the default. Tell me if one fits your needs 🙂

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

Sidebar

Related Questions

I'm using the javascript module pattern, and have this so far: var APP; if(APP
When using the module pattern in javascript how should constructors be defined, if at
I'm using the module pattern , one of the things I want to do
I am using the Javascript Module Pattern to try and implement C# enumeration-like functionality.
I am trying to organize my code using the revealing module pattern. I have
I've been using javascript module pattern for while. I showed an example of a
I've been writing some pseudo-oop JavaScript using the Module pattern. I'm curious, what is
when using the module pattern in js i've notice that the main benefits are
Using Python module re, how to get the equivalent of the \w (which matches
I'm creating a User Interface using PyQt4 module . the problem i'am facing is

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.