Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how to chain event callbacks

Reply
Thread Tools

how to chain event callbacks

 
 
fxn
Guest
Posts: n/a
 
      06-27-2007
I want to add programmatically a function call to some onkeypress,
taking into account that perhaps it already has something assigned.
This addition is done on window load with the following code (it has
some Prototype mixed in but it is minimal):

function add_autofit() {
$$('.autofit').each(function (e) {
var atf = function (input) { autofit(input); };

var current_onkeypress = Prototype.emptyFunction;
if (e.onkeypress)
current_onkeypress = e.onkeypress;

e.onkeypress = function () { current_onkeypress(this);
atf(this); };
autofit(e);
});
}
Event.observe(window, 'load', add_autofit);

Looks like I don't understand the scope of this, because autofit
alerts it has received the window object as input. I do not know how
to get that right, would you help please?

-- fxn

PS: I am not using listeners because I need to invoke onchange
programmatically afterwards.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-27-2007
On Jun 27, 9:33 pm, fxn <(E-Mail Removed)> wrote:
> I want to add programmatically a function call to some onkeypress,
> taking into account that perhaps it already has something assigned.
> This addition is done on window load with the following code (it has
> some Prototype mixed in but it is minimal):


Not "minimal" at all - it uses Prototype.js's Prototype object, $$()
CSS selector and Event function objects and .each Array method
extension. The scope issue you are having is common when attaching
handlers and using the this keyword.

You can get help specific to Prototype.js at the Ruby on Rails:
Spinoffs group:

<URL: http://groups.google.com.au/group/ru...s?lnk=li&hl=en
>



--
Rob

 
Reply With Quote
 
 
 
 
fxn
Guest
Posts: n/a
 
      06-27-2007
On Jun 27, 2:05 pm, RobG <(E-Mail Removed)> wrote:
> On Jun 27, 9:33 pm, fxn <(E-Mail Removed)> wrote:
>
> > I want to add programmatically a function call to some onkeypress,
> > taking into account that perhaps it already has something assigned.
> > This addition is done on window load with the following code (it has
> > some Prototype mixed in but it is minimal):

>
> Not "minimal" at all - it uses Prototype.js's Prototype object, $$()
> CSS selector and Event function objects and .each Array method
> extension. The scope issue you are having is common when attaching
> handlers and using the this keyword.
>
> You can get help specific to Prototype.js at the Ruby on Rails:
> Spinoffs group:


I know that group, but my guess is that this comes from a
misunderstunding about:

* The scope of the this keyword in the plain JavaScript closures

* How write the analogous of onchange="foo(this)" in plain JavaScript

I think that doesn't work because the non-Prototype code, which is the
key of my question, has the concepts wrong. I could reproduce this
with no Prototype code. Just attaching that to window.onload and
iterating over elements of autofit class with regular calls.

-- fxn

 
Reply With Quote
 
fxn
Guest
Posts: n/a
 
      06-27-2007
On Jun 27, 2:39 pm, fxn <(E-Mail Removed)> wrote:
> On Jun 27, 2:05 pm, RobG <(E-Mail Removed)> wrote:
>
> > On Jun 27, 9:33 pm, fxn <(E-Mail Removed)> wrote:

>
> > > I want to add programmatically a function call to some onkeypress,
> > > taking into account that perhaps it already has something assigned.
> > > This addition is done on window load with the following code (it has
> > > some Prototype mixed in but it is minimal):

>
> > Not "minimal" at all - it uses Prototype.js's Prototype object, $$()
> > CSS selector and Event function objects and .each Array method
> > extension. The scope issue you are having is common when attaching
> > handlers and using the this keyword.

>
> > You can get help specific to Prototype.js at the Ruby on Rails:
> > Spinoffs group:

>
> I know that group, but my guess is that this comes from a
> misunderstunding about:
>
> * The scope of the this keyword in the plain JavaScript closures
>
> * How write the analogous of onchange="foo(this)" in plain JavaScript
>
> I think that doesn't work because the non-Prototype code, which is the
> key of my question, has the concepts wrong. I could reproduce this
> with no Prototype code. Just attaching that to window.onload and
> iterating over elements of autofit class with regular calls.
>
> -- fxn


OK, I narrowed that problem to an even more simple and pure JavaScript
example, see
http://groups.google.com/group/comp....d97ed21d66de9a.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
event chain in asp.net Paul F ASP .Net 2 07-20-2008 08:00 PM
Event Machine - callbacks from different connections Tim Conner Ruby 1 03-03-2008 12:32 PM
multiple callbacks per function pointer / event glennpierce C Programming 1 02-14-2005 10:12 PM
COM event callbacks ivan_r_moore Python 0 11-05-2003 10:12 AM



Advertisments