Hi Pascal,

On 9/26/06, Pascal <[EMAIL PROTECTED]> wrote:
> hey gang--
>
> I have a project which is using JQuery as its javascript foundation.
> In that project have a page of modules (div class="pmodule") that i
> would i like extend using Function.prototype or the JQuery equivalent.
>
> what i'd like to do is create a prototype object (with some custom
> methods) and then convert each of those DOM elements into an instance
> of that prototype object, so that each module on the page will then
> attain those custom methods.
>
> here's a simplified version of what i have so far:
>
> function Pmodule() {
>
> };
>
> Pmodule.prototype = {
>         doSomething: function() {
>                 alert(1);
>         },
>         doSomethingElse: function(somevalue) {
>                 alert(somevalue);
>         }
> };
>
> $.fn.pmodule = function() {
>         return this.each(function() {
>                 // here i'd like to convert each <div class="pmodule"> into an
> instance of the Pmodule object
>                 // so it will have all the private methods defined in that 
> object
>                 // but I'm not sure where to go from here
>         });
> };
>
> $(document).ready(
>         function() {
>                 $('.pmodule').pmodule();
>         }
> );
>
>
> any help at all would be greatly appreciated. if this can be done
> differently/more efficiently in some other way please feel free to
> turn this upside down.

You can do this:

$.fn.pmodule = function() {
        return this.each(function() {
            jQuery(this, new Pmodule());
        });
};

This code will make the Pmodule methods and properties available to
every selected element.

Unfortunately, this will be useless if you don't attach some event handlers.

I was thinking of a good way to convert any element into a widget this
very evening. What I've thought and just tried is:

        function Pmodule(el) {
                $(el).click(function(e) {
                        this.doSomething();
                        e.preventDefault();
                });
                
        };

        Pmodule.prototype = {
               doSomething: function() {
                       alert(1);
               }
        };

        $.fn.pmodule = function() {
               return this.each(function() {
                        jQuery.extend(this, new Pmodule(this));
               })
        };

        $(document).ready(
               function() {
                       $('a').pmodule();
               }
        );

Is this what you are looking for?

-- 
Choan
<http://choangalvez.nom.es/>

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to