views:

46

answers:

1

I have developed below plug-in

(function($) {

 $.fn.addressSearch = function(settings) {

    settings = jQuery.extend({
        searchClass: "quickSearch",                             
        checkElement: "href",                                   
        dataElement: "data",                                   
        countryListClass: "countryList",                       
        countryCode: "11455",                                   
        errorMsg: "You can only search for address in the UK.",
        houseNumberClass: "TextboxHouseNumber",                 
        postcodeClass: "postcode",                              
        addressLine1Class: "addSearchLine1",                    
        addressLine2Class: "addSearchLine2",                    
        addressLine3Class: "addSearchLine3",                   
        addressTownCityClass: "addTownCity",                    
        ajaxUrl: "/WebService/addressLook",               
        submitType: "POST",                                     
        dataType: "xml",                                       
        parameters: "",                                         
        addressProcessURL: "",                                  
        callbackFunctionSingleAddress: selectAddress, //Callback 1       
        callbackFunctionMultipleAddress: quickBoxSearch, //Callback 2       
        useExternalProcessPage: false,                          
        validateCountry: true                                   

    }, settings);

    var jQueryMatchedObj = this;                                

    function _initialize() {
        _startModal(this, jQueryMatchedObj);                    
        return false;                                           
    }
    function _startModal(objClicked, jQueryMatchedObj) {
        $j(objClicked).addClass(settings.searchClass);

        var countryList = "." + settings.countryListClass + "";

        if (settings.validateCountry) {
            if ($j(countryList) && $j(countryList).val() != settings.countryCode) {
                alert(settings.errorMsg);
                return false;
            }
        }

        if (settings.parameters) {
            $j.ajax({
                url: settings.ajaxUrl,
                type: settings.submitType,
                dataType: settings.dataType,
                data: settings.parameters,
                success: function(res) {
                    var addresses = eval(res.getElementsByTagName('string')[0].firstChild.data);
                    if (addresses.length == 0)
                        alert('Your address could not be found, please enter it manually');
                    else if (addresses.length == 1) {
                        //Callback 1 and parameters set here
                        settings.callbackFunctionSingleAddress(
                            addresses[0].addressLine1,
                            addresses[0].addressLine2,
                            addresses[0].addressLine3,
                            addresses[0].town,
                            settings.TextboxHouseNumber,
                            settings.postcodeClass,
                            settings.addressTownCityClass,
                            settings.addressLine1Class,
                            settings.addressLine2Class,
                            settings.addressLine3Class
                            );
                    } else if (addresses.length > 1) {
                        //Callback 2 and parameters set here
                        settings.callbackFunctionMultipleAddress(
                            settings.callbackFunctionSingleAddress,
                            addresses,
                            settings.useExternalProcessPage,
                            settings.TextboxHouseNumber,
                            settings.postcodeClass,
                            settings.addressTownCityClass,
                            settings.addressLine1Class,
                            settings.addressLine2Class,
                            settings.addressLine3Class
                            );
                    }
                }
            });
        }
        return false;
    }
    return this.unbind('click').click(_initialize);
}

})(jQuery);

Above works fine without any problem. I call this with code below

$('#mydiv').click(function() {
  $(this).addressSearch(/* ... */);
});

However now I want to extend this even further with the passing both callback functions and parameters in the settings for the plugging so the plugging will be more robust.

how do I do this, basically I want to pass

                    settings.callbackFunctionSingleAddress(
                        addresses[0].addressLine1,
                        addresses[0].addressLine2,
                        addresses[0].addressLine3,
                        addresses[0].town,
                        settings.TextboxHouseNumber,
                        settings.postcodeClass,
                        settings.addressTownCityClass,
                        settings.addressLine1Class,
                        settings.addressLine2Class,
                        settings.addressLine3Class
                        );

AND

                    settings.callbackFunctionMultipleAddress(
                        settings.callbackFunctionSingleAddress,
                        addresses,
                        settings.useExternalProcessPage,
                        settings.TextboxHouseNumber,
                        settings.postcodeClass,
                        settings.addressTownCityClass,
                        settings.addressLine1Class,
                        settings.addressLine2Class,
                        settings.addressLine3Class
                        );

as parameters on the click event of a div. So it would look like,

$('#mydiv').click(function() {
  $(this).addressSearch({
    callbackFunctionSingleAddress: callbackFuntion(param1, param2)
  });
});

Above is the idea. Is this possible? Please help

A: 

If I'm reading this right, all you need to do is wrap the callbackFunction in a function block:

$('#mydiv').click(function() {
  $(this).addressSearch({
    callbackFunctionSingleAddress: function() { callbackFuntion(param1, param2); }
  });
});
Ken Earley