Escriba un complemento jQuery que devuelva valores

Estoy escribiendo un complemento jQuery que almacena algunos datos en algunos casos.

Me gustaría escribirlo de una manera muy flexible, donde pueda cambiar un parámetro de entrada para obtener algún valor almacenado por el complemento.

Explicación:

Cuando llamo$("#any").myPlugin(), mi complemento se inicializa creando undiv y algoa dentro. Al hacer clic en una lo guardará.index() utilizando la.data() método. Si llamo$("#any").myPlugin("getSelection") entonces me gustaría obtener el valor almacenado con.data().

Lo que probé:

(function ($) {
    $.fn.myPlugin = function (action) {
        if (action == null) action = "initialize";

        return this.each(function ($this) {
            $this = $(this);

            if (action == "initialize") {
                $this.html('<div></div>');
                var div = $("div", $this);

                div.append('<a>A</a>').append('<a>B</a>').append('<a>C</a>');

                div.children("a").each(function (i) {
                    $(this).click(function (event) {
                        // Here I store the index.
                        $this.data($(this).index());
                        event.preventDefault();
                        return false;
                    });
                });

                return $this;
            } else if (action == "getSelection") {
                // With this action, I tried to get the stored value.
                return $this.data("selectedValue");
            }
        });
    };
})(jQuery);

Llamada simple para crear los elementos:

$("#someElement").myPlugin();

Y aquí intenté obtener el índice, sin éxito:

alert($("#someElement").myPlugin("getSelection"));

Entonces, ¿es posible hacer lo que intento?

Respuestas a la pregunta(2)

Su respuesta a la pregunta