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

Victor KimuraSEO, Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
greetings  vkimura2007,, , I do not see the 'onChangeMedicalDirective' function as "anonymous", the 'onChangeMedicalDirective' is a javascript EVENT as "onchange", the function that is triggered by this event is defined as
function() {
  var iMedicalDirective = $('input#' + this + ':checked').length;
  if ( iMedicalDirective >=  1 ) {
     $('#' + this + '_spouse').prop('disabled', false);
     } else {
     $('#' + this + '_spouse').attr('checked', false);
     }
  }

Open in new window


you can make a Function OUT side of this code block and then call that function in the defined event function -
        
        /**
         * PubSubs subscribers: when user clicks on input id="medical_directive".
         * This enables/disables id="medical_directive_spouse"
         */
        MyUT.oPubSubs.subscribe( 'onChangeMedicalDirective', function() {
        doSomething (this) ;
        }, 'medical_directive');

 // the function BELOW goes Out Side of your above OBJECT container

function doSomething (obj) { // obj is the "this"
alert("doSomething EVENT function");
}

Open in new window


because in javascript all functions are Variables, you can do this other ways, BUT you can NOT use your -
    $('').enableSpouseCheckbox(this);

you can also designate a Jquery function to call , but you do not need a jquery function for this to operate


Why do you want to do this? as you can change the code in the function code block, the same that you can do in your own defined function, I see no advantage?
as questions if you need more info.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Victor KimuraSEO, Web DeveloperAuthor Commented:
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.
0
Slick812Commented:
???, It is Good to not do repetative code. . OK, This gets to be what I think is complex, as JQuery IS Javascript!, BUT Javascript IS NOT Jquery. You can use the Jquery syntax -
           $.fn.doSomething = function(par) {
OR
             jQuery.fn.doSomething = function(par1,par2) {

BUT this EXTENDS (adds to) the available METHODS for a Jquery Object, and there is a difference between a "stand alone" Function and an object Method. I can NOT see how extending the Jquery Object with your method, can be a help for this.
And to try to explain all of the subtleties of javascript programming (you can also "prototype" in JS to add methods), I can not even try, because they are many.
Please look at this page -
     http://www.sitepoint.com/5-ways-declare-functions-jquery/
which may show you some basics, but you should maybe use just a stand alone function , UNLESS there is a Jquery Object that you must use and add a method to it.
In stand alone functions you can USE ALL of Jquery functionality , and then any OTHER (your own code functions and objects), so it does not limit you.

Also look at the Jquery documentation for it's EVENT methods, there are some additional things that can be used in events.
0
Victor KimuraSEO, Web DeveloperAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.