In script#, I have the following:
jQuery.Select("#someId").Trigger("item-added", new object[]{"ball"} );
This compiles into the following Javascript:
$('#someId').trigger('item-added', [ 'ball' ]);
The above code works fine. The problem comes in declaring an event handler that can receive the argument array.
In order for my handler to receive the argument array, it would have to have a signature as such:
public void OnItemAdded(jQueryEvent jqe, Object[] args)
However, the only type available is:
public delegate void jQueryEventHandler(jQueryEvent e)
And as such, when the handler is called, the args are nowhere to be found.
I’ve read about the other methods of passing arguments to event handlers on the jQuery api docs ( http://api.jquery.com/trigger ) and they don’t seem to be possible in script#.
Adding to DuckMaestro’s suggestion –
The sources for Script.jQuery are on github … they’re there so people can change it, not be blocked, and not have to resort to things like InvokeMethod, Script.Literal and other such sub-optimal workarounds.
The reason the signature is not like that to begin with is every event handler shouldn’t need to define extra parameters in its signature when not needed.
Option 1 –
Create a different delegate signature – like jQueryEventHandler. And then create overloads of Bind, and Trigger that use your new signature. Its not hard, esp. if you look at the existing implementation source as a reference.
Option 2 –
Use the other pattern suggested by examples at http://api.jquery.com/trigger.
Specifically add properties to jQueryEvent and then create the derived class where you trigger the event, set properties on it for your specific scenarios. And then in the event handler cast the jQueryEvent back to the specific type.
The nice thing about this is it is consistent with the DOM… in terms of what you’d do with document.createEvent, and dispatchEvent. As such I prefer this option, besides the fact that it doesn’t need overloads of bind/trigger for every specific scenario.
The one thing you’ll run into right now is jQueryEvent itself is sealed right now, but I am happy to make that unsealed to support these scenarios (be sure to log a bug on github so it can be tracked). In the mean-time to be unblocked, you have the sources to make it unsealed on your end.
If you really don’t want to get into rebuilding Script.jQuery, a temporary workaround (until you have a build where you can start deriving from) is the following: