1

I'm trying to write my first jquery plugin but I'm running into some difficulties.

Code snippets:

Here I am initializing my plugin and setting the function that should execute when MyEvent occurs.

$("#myMap").MyPlugin({ myEvent: function() { alert("test"); } });

And this is the function in my plugin script

function setEvents(options) {
            $.each(options, function(event, fn) {
                if (typeof (fn) == 'function') {
                    if (event == "myEvent") {
                        $(myDiv).click(fn);
                    }
                }
            });
        }

This works fine, but I want to retrieve information from the plugin while the even occurs. Basically I want to receive a parameter I have set in that "setEvents" function.

$("#myMap").MyPlugin({ myEvent: function(myParam) { alert(param); } });

I cant seem to figure this one out, can someone point me in the right direction.

UPDATE: a more complete snippet of my current plugin script

(function($) {
    var defaults = {
        width: 300,
        height: 300,
        myEvent: null
    };

    $.fn.myPlugin = function(options) {
        var options = $.extend(defaults, options); 
        var myDiv = $(this);
        return this.each(function() {
            setEvents(options);
        });
})(jQuery);
4
  • This doesn't seem to be complete code. please post complete code. Commented Jul 2, 2012 at 9:20
  • I don't follow. What precisely do you want what to be able to access? can you edit to provide a clearer example? Commented Jul 2, 2012 at 9:21
  • @BBQ, do you want a specific parameter to be passed to your handler, or do you want it to receive the whole options hash? Commented Jul 2, 2012 at 9:36
  • It's not the neatest code I've ever written since I'm trying some things out. But handler only needs options.myEvent which would make the loop unnecessary. Commented Jul 2, 2012 at 9:45

1 Answer 1

2

Since the myEvent event is mapped to a click event by your plugin, you can pass a parameter map to click() in order for it to be ultimately passed to your handler:

if (typeof(fn) == "function") {
    if (event == "myEvent") {
        $(myDiv).click({
            param: myParam
        }, fn);
    }
}

Then you can write:

$("#myMap").MyPlugin({
    myEvent: function(event) {
        alert(event.data.param);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Frederic. It works with a little tweak. I had to change it to 'args.data.param'. Is there an easy way to go around the parameter map thing, and just receive a string as parameter?
@BBQ, unfortunately, jQuery handlers are designed to receive a single parameter hash that is transmitted through the event object (I used the wrong name in my answer, this is now fixed). You can pass a single value in that hash, but there has to be a hash around it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.