Avatar of Victor Kimura
Victor Kimura
Flag for Canada asked on

How to pass arguments of pubsub to anonymous function

I have this pubsub (from a tutsplus videon on JS OOP). This works with the anonymous function in the subscriber method:

        
        /**
         * PubSubs subscribers: when user clicks on input id="medical_directive".
         * This enables/disables id="medical_directive_spouse"
         */
        MyUT.oPubSubs.subscribe( 'onChangeMedicalDirective', function() {
            var iMedicalDirective = $('input#' + this + ':checked').length;

            if ( iMedicalDirective >=  1 ) {
                //enable input id='medical_directive_spouse'
                $('#' + this + '_spouse').prop('disabled', false);
            } else {
                //uncheck the id='medical_directive_spouse'
                $('#' + this + '_spouse').attr('checked', false);
            }
        }, 'medical_directive');

Open in new window


But I'm trying to extract that anon function. But I don't know how to pass the variable 'this' which is the third argument in the subscribe method above (i.e. 'medical_directive').

I have this but it doesn't work. How can I make this work?

	
        /**
	 * PubSubs subscribers: when user clicks on input id="medical_directive".
	 * This enables/disables id="medical_directive_spouse"
	 */
	MyUT.oPubSubs.subscribe( 'onChangeMedicalDirective', $('').enableSpouseCheckbox(this), 'medical_directive');

    $.fn.enableSpouseCheckbox = function(that) {
        var iMedicalDirective = $('input#' + that + ':checked').length;

        if ( iMedicalDirective >=  1 ) {
            //enable input id='medical_directive_spouse'
            $('#' + that + '_spouse').prop('disabled', false);
        } else {
            //uncheck the id='medical_directive_spouse'
            $('#' + that + '_spouse').attr('checked', false);
        }
    };

Open in new window

     
Blessings<><,
Victor
 /**
         * PubSubs subscribers: when user clicks on input id="medical_directive".
         * This enables/disables id="medical_directive_spouse"
         */
        MyUT.oPubSubs.subscribe( 'onChangeMedicalDirective', function() {
            var iMedicalDirective = $('input#' + this + ':checked').length;

            if ( iMedicalDirective >=  1 ) {
                //enable input id='medical_directive_spouse'
                $('#' + this + '_spouse').prop('disabled', false);
            } else {
                //uncheck the id='medical_directive_spouse'
                $('#' + this + '_spouse').attr('checked', false);
            }
        }, 'medical_directive');

Open in new window

JavaScriptjQueryAJAX

Avatar of undefined
Last Comment
Victor Kimura

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Member_2_248744

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Victor Kimura

ASKER
Hi Slick812,

Ok, I see.

So, how do I "you can also designate a Jquery function to call , but you do not need a jquery function for this to operate"?

The reason I want to extract that code to its own defined function because there were other events that was using similar code in that function block. :) I just saw repetitive code.
SOLUTION
Member_2_248744

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Victor Kimura

ASKER
Hmm...

yeah, I see. The jQuery object method didnt' work. It's in the first post:

MyUT.oPubSubs.subscribe( 'onChangeMedicalDirective', $('').enableSpouseCheckbox(this), 'medical_directive');

I will try the standalone function though.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy