Justin Harris
Justin Harris

Reputation: 289

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">

Upvotes: 3

Views: 10022

Answers (3)

Johan
Johan

Reputation: 35213

This should do the trick:

var that = this;

$(this.schedulePickerDiv).on("click", "#addWalk", function(e){
    that.openAddWalkDialog.call(this, e);
});

ReservationSchedulePicker.prototype.openAddWalkDialog = function(event) {
    console.log(event, $(this).data('day'));
}

Upvotes: 1

Elise Chant
Elise Chant

Reputation: 5196

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));

Upvotes: 1

Dalorzo
Dalorzo

Reputation: 20014

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>

Upvotes: 2

Related Questions