views:

27

answers:

3

I need to raise a couple of events with parameters within a jquery plugin, looked at similar questions, but cant seem to see what I am looking for? ANy basic examples out there?

+1  A: 

You mean this?

$('#theTarget').trigger('event-name', ['hello', 'world']);

I don't think I know what "consume it on the front-end" means ...

Pointy
Thanks, will have a look...My terminology is a bit off there, I think I am looking for a "callback" from my jquery plugin which needs be run in the page (what I reffered to as the front-end)
Mark Redman
A: 

I found the answer here: another stackoverlow answer

Mark Redman
do i just delete my own question now...
Mark Redman
+1  A: 

if i understand well you want to provide some callbacks, being able to call them as

$(this).plugin({
    onInit: function(var) { alert(var) },
    onClick: function(var) { alert(var + '!') }
});

the way i do this is by adding an option 'onInit' to the plugin and call options.onInit(var1, var2, ...) inside the plugin in the place where i want it executed

$.fn.plugin = function(options) {
    var o = $.extend({}, $.fn.plugin.defaults, options);

    return this.each(
        function() {
            o.onInit('initializing...');

            $(this).bind('click', function(e) {
                onClick('clicked');
            });
        }
    );
}

now the former code will execute alert('initializing...'); on init and alert('clicked!'); on click

i am not sure this code follows any official syntax, but it works well

if you just want to bind some custom events it is simple as described in http://api.jquery.com/bind/, just

$(this).bind('customevent', function() {
    alert('hi');
});

then use

$(this).trigger('customevent');
dimvic