2

I have a widget created using jquery ui widget factory. I'm trying to pass some data to my custom event like this

this._trigger('myCustomEvent', ['Test', 'Test2']);

but on my handler I'm not sure how to access custom parameters

onMyCustomEvent: function(event, ui) {
   ...
}

I've tried adding parameter to my handler

onMyCustomEvent': function(event, ui, param1, param2) {...}

but that didn't work: param1 and param2 are undefined

StackOverflower
  • 5,463
  • 13
  • 58
  • 89
  • When you tried added the parameters to your handler, did you also make sure you take the additional params out of the array and put them into the proper spots when you attempted to trigger it? – ayyp Jun 07 '13 at 20:24
  • @Andrew Peacock: I'm afraid I have no idea what you're talking about. What I showed is all the relevant code I have :) – StackOverflower Jun 07 '13 at 20:29
  • I tested with a single parameter and didn't work. `this._trigger('myCustomEvent', 'Test');` and `onMyCustomEvent': function(event, ui, param1) {...}`` – StackOverflower Jun 07 '13 at 20:32

1 Answers1

4

You need to provide the event param on _trigger. The param can be null.

this._trigger('myCustomEvent', null, ['Test', 'Test2']);

See: http://api.jqueryui.com/jQuery.widget/#method-_trigger

Note: When providing data, you must provide all three parameters. If there is no event to pass along, just pass null.

Example: http://jsfiddle.net/4Za4m/

$.widget( "jsr.exampleWidget", {
    _create: function() {
        this.element.click($.proxy(this._handleClick,this));
    },

    _handleClick: function(event) {
        event.preventDefault();
        this._trigger('myCustomEvent', null, ['Test', 'Test2']);
    }    
});

var handleCustomEvent = function(event,param1,param2) {
    alert(param1);
    alert(param2);
};

$("#clickable").exampleWidget({myCustomEvent : handleCustomEvent});
James Ross
  • 758
  • 5
  • 14