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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:09:15+00:00 2026-05-20T10:09:15+00:00

I am writing a small module which will have several different aspects to it,

  • 0

I am writing a small module which will have several different aspects to it, all based around ajax calls. I want to allow the main ajax functions i.e beforeSend, success, complete etc (I am using jQuery) to be customizeable.

What is the best way to do this?

I currently have an options object in the module which can be extended with an options object passed to an init function. In here, I am passing a ‘callbacks’ object with nested ojects for each different type of action as in…

        var insertCallbacks = {

        before : function() {

        },
        success : function() {

        },
        error : function() {

        },
        complete : function() {

        }
    };

    var updateCallbacks = {
        before : function() {

        },
        success : function() {

        },
        error : function() {

        },
        complete : function() {

        }
    };

    var callbacks =  {
        insert : addCallbacks,
        update : removeCallbacks    
    };

   MY_MODULE.init( {callbacks : callbacks} );

The problem is this then becomes a bit messy, testing for the existence of each of these methods on the ajax callbacks.

Can anyone offer any advice on a good/better pattern for this.

  • 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-20T10:09:15+00:00Added an answer on May 20, 2026 at 10:09 am

    I would go with custom events rather than callbacks. So in your module you will have code like:

    MY_MODULE.trigger('updateComplete');
    

    and in all parts outside of module (as well as inside if needed), you bind handlers (now they are callbacks):

     MY_MODULE.bind('updateComplete', function() { 
        alert('update completed'); 
     } );
    

    Custom events in jQuery open doors to complex behaviors, or google any other article. Custom events will help you to keep code structured, and easier to test

    ADD ON: with callbacks you need always to check if there is any, so you code becomes

    if ( callbacks && callbacks.insert ) {
        callbacks.insert();
    }
    

    improving your module functionality, and enhancing it, one day you get a situation that few callbacks should be passed for the same situation (e.g. two entities or UI components are interested in module ‘updating’)… It will make your job too difficult. With events you always have one code line

    MY_MODULE.trigger('updateComplete');
    

    with no conditions to check if there is any handler attached (interested in an event), and you can have as many as needed handlers for the same event.

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

Sidebar

Related Questions

I'm writing a small tool in C# which will need to send and receive
I'm writing a small blog module. I want the users to be able to
Im just writing a small Ajax framework for re-usability in small projects and i've
I'm writing a small agent in java that will play a game against other
I am writing a small app which I need to test with utf-8 characters
When writing a library or the public API of a module that will be
I'm writing a small Django-based frontend to collect and graph internet usage statistics. Currently,
I am currently writing a small calendar in ASP.Net C#. Currently to produce the
I'm writing a small web server in Python, using BaseHTTPServer and a custom subclass
I'm writing a small article on humanly readable alternatives to Guids/UIDs, for example those

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.