Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery proxy passing parameters

How can you pass a string via proxy when binding event handlers? I want to pass a data attribute that is attached to the target handler to a method of an object. Is this possible?

function ReservationSchedulePicker(reservationType){

//Reservation Type Accepted Use: 'routine' || 'vacation'
this.reservationType = reservationType;

//DIV for Schedule Picker
this.schedulePickerDiv = $("#schedulePicker");

//Add Event Handler to Anchor
$(this.schedulePickerDiv).on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this));
}

}
    
ReservationSchedulePicker.prototype.openAddWalkDialog = function(event, day) {
//Trying to pass the data-day value to this function using proxy



}


     //I need the data-day value inside of openAddWalkDialog Is this possible?
     <a href="#" id="addWalk" data-day="monday">
like image 752
Justin Harris Avatar asked Aug 13 '26 19:08

Justin Harris


2 Answers

Is this what you are looking for:

<script type="text/javascript">
    function ReservationSchedulePicker(reservationType){
        this.reservationType = reservationType;
        this.schedulePickerDiv = $("#schedulePicker");
        this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this));
    }

    ReservationSchedulePicker.prototype.openAddWalkDialog = function(event) {
        event.preventDefault();
        alert(this.reservationType);
    }
    $(document).on('ready',function(){
        var x = new ReservationSchedulePicker('hello world');
    });

</script>
<div id="schedulePicker">
    <a href="#" id="addWalk" data-day="monday">Hello</a>
</div>

Update 1: Based additional details provided in comments

<script type="text/javascript">
    function ReservationSchedulePicker(reservationType){
        this.reservationType = reservationType;
        this.schedulePickerDiv = $("#schedulePicker");
        this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this, $("#addWalk").attr('data-day') ));
    }



    ReservationSchedulePicker.prototype.openAddWalkDialog = function(attr, event) {
        event.preventDefault();
        alert(this.reservationType + '=>'+ attr);
    }
    $(document).on('ready',function(){
        var x = new ReservationSchedulePicker('hello world');
    });

</script>
<div id="schedulePicker">
    <a href="#" id="addWalk" data-day="monday">Hello</a>
</div>

Update 2

<script type="text/javascript">
    function ReservationSchedulePicker(reservationType){
        this.reservationType = reservationType;
        this.schedulePickerDiv = $("#schedulePicker");
        this.schedulePickerDiv.on( "click", "#addWalk", $.proxy(this.openAddWalkDialog, this ));
    }



    ReservationSchedulePicker.prototype.openAddWalkDialog = function( event) {
        event.preventDefault();

        alert(this.reservationType + '=>'+ ($(event.target).data('day'))) ;
    }
    $(document).on('ready',function(){
        var x = new ReservationSchedulePicker('hello world');
    });

</script>
<div id="schedulePicker">
    <a href="#" id="addWalk" data-day="monday">Hello</a>
</div>
like image 94
Dalorzo Avatar answered Aug 16 '26 08:08

Dalorzo


What about something like reference arguments to proxy:

$(this.schedulePickerDiv).on('click', '#addWalk', $.proxy(this.openAddWalkDialog, this, { foo: bar }));

Or on the event.data object (event.data.foo):

$(this.schedulePickerDiv).on('click', '#addWalk', { foo: bar }, $.proxy(this.openAddWalkDialog, this));
like image 20
Elise Chant Avatar answered Aug 16 '26 10:08

Elise Chant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!