Link to home
Start Free TrialLog in
Avatar of Victor Kimura
Victor KimuraFlag 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

ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.