extending prototype's classes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

extending prototype's classes

Jeremy Kitchen
Hi, I'm relatively new to javascript programming, and OOP in general, so
please be nice to me ;)

My question is:
I'd like to extend the Form class in prototype to have a Disable function that
will disable all of the fields in a form.

I've already written a function to do this, but I'd like to include it in my
own library so I can access it easily and also so it lies within a namespace.

I know I can just modify prototype.js to do this, but I'd rather not, if
possible, so I can easily update prototype without having to worry about
adding my code back in.

I have something like this in my html files:
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript" src="libkitchen.js"></script>

if possible, I'd like to put the extension of the class into libkitchen.js

If nothing else, if you could toss me in the direction of something to read or
an example to look at, I will figure it out :)

Thanks in advance, and thanks for prototype, it really is an amazing example
of javascript :)

-Jeremy

_______________________________________________
Rails-spinoffs mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: extending prototype's classes

Andrew Kaspick
The prototype lib already has the enable and disable functions for the
Form class to do what you want.

On 12/15/05, Jeremy Kitchen <[hidden email]> wrote:

> Hi, I'm relatively new to javascript programming, and OOP in general, so
> please be nice to me ;)
>
> My question is:
> I'd like to extend the Form class in prototype to have a Disable function that
> will disable all of the fields in a form.
>
> I've already written a function to do this, but I'd like to include it in my
> own library so I can access it easily and also so it lies within a namespace.
>
> I know I can just modify prototype.js to do this, but I'd rather not, if
> possible, so I can easily update prototype without having to worry about
> adding my code back in.
>
> I have something like this in my html files:
> <script type="text/javascript" src="prototype.js"></script>
> <script type="text/javascript" src="libkitchen.js"></script>
>
> if possible, I'd like to put the extension of the class into libkitchen.js
>
> If nothing else, if you could toss me in the direction of something to read or
> an example to look at, I will figure it out :)
>
> Thanks in advance, and thanks for prototype, it really is an amazing example
> of javascript :)
>
> -Jeremy
>
>
> _______________________________________________
> Rails-spinoffs mailing list
> [hidden email]
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
>
>
>
_______________________________________________
Rails-spinoffs mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Reply | Threaded
Open this post in threaded view
|

Re: extending prototype's classes

Jeremy Kitchen
On Thursday 15 December 2005 17:56, Andrew Kaspick wrote:
> The prototype lib already has the enable and disable functions for the
> Form class to do what you want.

hmm... you're right :)

well, let's say hypothetically I want to extend one of prototype's classes
with a function, how would I go about doing that?

-Jeremy

_______________________________________________
Rails-spinoffs mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: extending prototype's classes

Michael Schuerig
On Friday 16 December 2005 03:19, Jeremy Kitchen wrote:
> On Thursday 15 December 2005 17:56, Andrew Kaspick wrote:
> > The prototype lib already has the enable and disable functions for
> > the Form class to do what you want.
>
> hmm... you're right :)
>
> well, let's say hypothetically I want to extend one of prototype's
> classes with a function, how would I go about doing that?

Form.foo = function(element) {
  ...
};

Form.Element.bar = function(element) {
  ...
};

etc.

Note that these are *not* classes. A class is something that can have
exemplars. That's not the case with Form, Form.Element and all the
others. What you see here is the JavaScript way of implementing
namespaces. In prototype.js look for Class.create() to see cases where
classes are implemented. In general, JavaScript doesn't have classes as
language constructs. Instead, it has prototypes. Prototype.js uses
prototypes to create something resembling classes in other OOPLs.

Michael

--
Michael Schuerig                       Face reality and stare it down
mailto:[hidden email]        --Jethro Tull, Silver River Turning
http://www.schuerig.de/michael/
_______________________________________________
Rails-spinoffs mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Reply | Threaded
Open this post in threaded view
|

RE: Re: extending prototype's classes

Gregory Hill
In reply to this post by Jeremy Kitchen

> Note that these are *not* classes. A class is something that can have
> exemplars. That's not the case with Form, Form.Element and all the
> others.

I wish there was more consistency in scriptaculous in this regard.  Why
Sortable.create('el'), but new Effect.Highlight('el')?  I'm sure there's
a reason, but I couldn't fathom a guess.  I'm thinking maybe I'll make
an extension that creates classes to be used for things like Sortable.
It'll just be a wrapper into the other function calls.

Then I can say new Sortable('el') like I keep doing on accident, but
it'll actually work.

Greg
_______________________________________________
Rails-spinoffs mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Reply | Threaded
Open this post in threaded view
|

Re: Re: extending prototype's classes

Thomas Fuchs
That's because Sortables need to be updatable, and some Object (the  
Sortable object in this case) needs to hold information about which  
options where used for creating them.

Maybe it's something for scriptaculous 2 to have a more consistant  
API here.

-Thomas

Am 16.12.2005 um 16:05 schrieb Gregory Hill:

>
>> Note that these are *not* classes. A class is something that can have
>> exemplars. That's not the case with Form, Form.Element and all the
>> others.
>
> I wish there was more consistency in scriptaculous in this regard.  
> Why
> Sortable.create('el'), but new Effect.Highlight('el')?  I'm sure  
> there's
> a reason, but I couldn't fathom a guess.  I'm thinking maybe I'll make
> an extension that creates classes to be used for things like Sortable.
> It'll just be a wrapper into the other function calls.
>
> Then I can say new Sortable('el') like I keep doing on accident, but
> it'll actually work.
>
> Greg
> _______________________________________________
> Rails-spinoffs mailing list
> [hidden email]
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

_______________________________________________
Rails-spinoffs mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Reply | Threaded
Open this post in threaded view
|

RE: Re: extending prototype's classes

Gregory Hill
In reply to this post by Jeremy Kitchen
Makes sense, but perhaps you could have a Sortable class that appends
itself to a container class when instantiated.  Then I could still say
new Sortable and Sortable.initialize would take care of making sure it
got into the right container.

Anyhoo, I didn't mean offense by my comments, just frustration at typing
the wrong thing over and over :)

Greg

> -----Original Message-----
> From: [hidden email]
[mailto:rails-spinoffs-

> [hidden email]] On Behalf Of Thomas Fuchs
> Sent: Friday, December 16, 2005 8:16 AM
> To: [hidden email]
> Subject: Re: [Rails-spinoffs] Re: extending prototype's classes
>
> That's because Sortables need to be updatable, and some Object (the
> Sortable object in this case) needs to hold information about which
> options where used for creating them.
>
> Maybe it's something for scriptaculous 2 to have a more consistant
> API here.
>
> -Thomas
>
> Am 16.12.2005 um 16:05 schrieb Gregory Hill:
>
> >
> >> Note that these are *not* classes. A class is something that can
have
> >> exemplars. That's not the case with Form, Form.Element and all the
> >> others.
> >
> > I wish there was more consistency in scriptaculous in this regard.
> > Why
> > Sortable.create('el'), but new Effect.Highlight('el')?  I'm sure
> > there's
> > a reason, but I couldn't fathom a guess.  I'm thinking maybe I'll
make
> > an extension that creates classes to be used for things like
Sortable.

> > It'll just be a wrapper into the other function calls.
> >
> > Then I can say new Sortable('el') like I keep doing on accident, but
> > it'll actually work.
> >
> > Greg
> > _______________________________________________
> > Rails-spinoffs mailing list
> > [hidden email]
> > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
> _______________________________________________
> Rails-spinoffs mailing list
> [hidden email]
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
[hidden email]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs