I think I may be missing a fundamental concept I hope someone can help
me with. I have functions that won''t call other functions. Particularly
after an Ajax request.
In the following object the ajax request onComplete calls the
ajaxFetched function successfully. ajaxFetched tries to call function2
but function2 does not execute.
Any ideas or workarounds?
thanks
Kevin
o = Class.create();
o.prototype = {
initialize: function() {
this.fetchAjaxData();
},
fetchAjaxData: function(){
url="ajax/minimap.php";
pars="x=1&y=1";
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
});
},
ajaxFetched: function(){
alert(''ajaxFetched called'');
this.function2();
},
function2: function(){
alert(''function2 called'');
}
}
Event.observe(window, ''load'', function(){var O = new o();});
Yup you''re missing something fundamental... what''s happening
is a scope
issue. Functions in javascript are first class objects. That means
(normally) from within a function the term "this" refers to the
function
itself...
So in ajaxFetched you have...
ajaxFetched: function(){
alert(''ajaxFetched called'');
this.function2();
},
..."this.function2()" is trying to find a function called function2 as
a
member of the Object ajaxFetched. Since there is no ajexFetched.function2,
it fails.
Prototype gives us a very handy tool to overcome this "problem" (not
actually a problem but a designed feature of the language -- it wasn''t
originally purposed for class based OO)... called bind().
bind() ensures that the "this" in the object''s functions
refers to the
object itself, and not the function. In your example, change the line where
you attach ajexFetched to the onComplete event (as I have it below), and
everything should be good to go...
fetchAjaxData: function(){
url="ajax/minimap.php";
pars="x=1&y=1";
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched.bind(this)
});
},
I can all but gaurantee you that when the rest of the people on this list
wake up in the morning, you''ll get a slew of helpful comments about
"closures" and scope issues.
This is by far the most common question/problem we see here :-)
Welcome to OO javascript.
On 5/24/06, Kev''n
<falstaff-dxFKlcbaSNapwFb5G8XvHQ@public.gmane.org>
wrote:>
> I think I may be missing a fundamental concept I hope someone can help
> me with. I have functions that won''t call other functions.
Particularly
> after an Ajax request.
>
> In the following object the ajax request onComplete calls the
> ajaxFetched function successfully. ajaxFetched tries to call function2
> but function2 does not execute.
>
> Any ideas or workarounds?
>
> thanks
> Kevin
>
>
> o = Class.create();
> o.prototype = {
> initialize: function() {
> this.fetchAjaxData();
> },
>
> fetchAjaxData: function(){
> url="ajax/minimap.php";
> pars="x=1&y=1";
>
> var myAjax= new Ajax.Request(
> url,
> {
> method: ''get'',
> parameters: pars,
> onComplete: this.ajaxFetched
> });
> },
>
> ajaxFetched: function(){
> alert(''ajaxFetched called'');
> this.function2();
> },
>
> function2: function(){
> alert(''function2 called'');
> }
> }
>
> Event.observe(window, ''load'', function(){var O = new
o();});
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Ryan,
What''s the non prototyp-ish way to handle this?
What if we don''t want to use bind()?
Thanks in advance,
Mandy.
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]On
Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 10:28 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
Yup you''re missing something fundamental... what''s happening
is a scope issue. Functions in javascript are first class objects. That means
(normally) from within a function the term "this" refers to the
function itself...
So in ajaxFetched you have...
ajaxFetched: function(){
alert(''ajaxFetched called'');
this.function2();
},
..."this.function2()" is trying to find a function called function2 as
a member of the Object ajaxFetched. Since there is no ajexFetched.function2, it
fails.
Prototype gives us a very handy tool to overcome this "problem" (not
actually a problem but a designed feature of the language -- it wasn''t
originally purposed for class based OO)... called bind().
bind() ensures that the "this" in the object''s functions
refers to the object itself, and not the function. In your example, change the
line where you attach ajexFetched to the onComplete event (as I have it below),
and everything should be good to go...
fetchAjaxData: function(){
url="ajax/minimap.php";
pars="x=1&y=1";
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched.bind(this)
});
},
I can all but gaurantee you that when the rest of the people on this list wake
up in the morning, you''ll get a slew of helpful comments about
"closures" and scope issues.
This is by far the most common question/problem we see here :-)
Welcome to OO javascript.
On 5/24/06, Kev''n <
falstaff-dxFKlcbaSNapwFb5G8XvHQ@public.gmane.org> wrote:
I think I may be missing a fundamental concept I hope someone can help
me with. I have functions that won''t call other functions. Particularly
after an Ajax request.
In the following object the ajax request onComplete calls the
ajaxFetched function successfully. ajaxFetched tries to call function2
but function2 does not execute.
Any ideas or workarounds?
thanks
Kevin
o = Class.create();
o.prototype = {
initialize: function() {
this.fetchAjaxData();
},
fetchAjaxData: function(){
url="ajax/minimap.php";
pars="x=1&y=1";
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
});
},
ajaxFetched: function(){
alert(''ajaxFetched called'');
this.function2();
},
function2: function(){
alert(''function2 called'');
}
}
Event.observe(window, ''load'', function(){var O = new o();});
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
<http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
On Wednesday 24 May 2006 23:02, Maninder, Singh wrote:> Ryan, > > What''s the non prototyp-ish way to handle this? > > What if we don''t want to use bind()?it really depends. If the "this.function2" function doesn''t rely on state within the object, just turn it into a ''class method'' and call it directly. If it does, then you''ll have to use bind. bind is really just a fancy wrapper around apply(), so you can do it manually, but I find bind to be much more handy :) -Jeremy _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Jeremy Kitchen
Sent: Thursday, May 25, 2006 3:25 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
<< File: ATT1071742.dat >> << File: ATT1071743.txt >>
On Wednesday 24 May 2006 23:02, Maninder, Singh wrote:> Ryan,
>
> What''s the non prototyp-ish way to handle this?
>
> What if we don''t want to use bind()?
it really depends. If the "this.function2" function doesn''t
rely on state
within the object, just turn it into a ''class method'' and call
it directly.
If it does, then you''ll have to use bind.
bind is really just a fancy wrapper around apply(), so you can do it manually,
but I find bind to be much more handy :)
-Jeremy
Sigh....didn''t finish editing the code. Changed the this.ajaxFetched
call to my functor ...functor.
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Martinez, Andrew
Sent: Thursday, May 25, 2006 9:13 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] functions calling functions
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Jeremy Kitchen
Sent: Thursday, May 25, 2006 3:25 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
<< File: ATT1071742.dat >> << File: ATT1071743.txt >>
On Wednesday 24 May 2006 23:02, Maninder, Singh wrote:> Ryan,
>
> What''s the non prototyp-ish way to handle this?
>
> What if we don''t want to use bind()?
it really depends. If the "this.function2" function doesn''t
rely on state
within the object, just turn it into a ''class method'' and call
it directly.
If it does, then you''ll have to use bind.
bind is really just a fancy wrapper around apply(), so you can do it manually,
but I find bind to be much more handy :)
-Jeremy
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Hi, Mandy :-) Long time no talk, eh? Been very busy :-)
Anyway, like others have said already in this thread... without bind(), it
becomes a pretty messy block of code every time you want to do this...
I''m assuming the call to function2() requires knowledge of the state of
the
instance of the "o" object, otherwise as Jeremy stated, just make it
some
globally visible function.
Prototype''s bind() function is as follows:
Function.prototype.bind = function() {
var __method = this, args = $A(arguments), object = args.shift();
return function() {
return __method.apply(object, args.concat($A(arguments)));
}
}
In a nutshell, it returns a modified function, which itself returns the
original function applied to the object passed as the argument (optionally
with any additional arguments supplied passed in as well).
The "non-prototypish" way to do this would be to recreate something
similar
to the above code, wrapped in a closure, every time you want to do this. You
can do it one line though, but still ugly IMHO :-), but if you''re into
using
the lower level constructs as much as possible for some kind of cross
library interoperability or "future-proofing" concerns, then the
OP''s class
can be rewritten as follows...
o = Class.create();
o.prototype = {
initialize: function() {
this.fetchAjaxData();
},
fetchAjaxData: function(){
url="ajax/minimap.php";
pars="x=1&y=1";
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: function() {
this.ajaxFetched.apply(this); }
});
},
ajaxFetched: function(){
alert(''ajaxFetched called'');
this.function2();
},
function2: function(){
alert(''function2 called'');
}
}
On 5/25/06, Maninder, Singh
<mandiv-W2hqgAdRMsX2eFz/2MeuCQ@public.gmane.org>
wrote:>
> Ryan,
>
> What''s the non prototyp-ish way to handle this?
>
> What if we don''t want to use bind()?
>
> Thanks in advance,
> Mandy.
>
> -----Original Message-----
> *From:*
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
>
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]*On
Behalf Of *Ryan Gahl
> *Sent:* Thursday, May 25, 2006 10:28 AM
> *To:* rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> *Subject:* Re: [Rails-spinoffs] functions calling functions
>
> Yup you''re missing something fundamental... what''s
happening is a scope
> issue. Functions in javascript are first class objects. That means
> (normally) from within a function the term "this" refers to the
function
> itself...
>
> So in ajaxFetched you have...
>
> ajaxFetched: function(){
> alert(''ajaxFetched called'');
> this.function2();
> },
>
> ..."this.function2()" is trying to find a function called
function2 as a
> member of the Object ajaxFetched. Since there is no ajexFetched.function2,
> it fails.
>
> Prototype gives us a very handy tool to overcome this "problem"
(not
> actually a problem but a designed feature of the language -- it
wasn''t
> originally purposed for class based OO)... called bind().
>
> bind() ensures that the "this" in the object''s functions
refers to the
> object itself, and not the function. In your example, change the line where
> you attach ajexFetched to the onComplete event (as I have it below), and
> everything should be good to go...
>
> fetchAjaxData: function(){
> url="ajax/minimap.php";
> pars="x=1&y=1";
>
> var myAjax= new Ajax.Request(
> url,
> {
> method: ''get'',
> parameters: pars,
> onComplete: this.ajaxFetched.bind(this)
> });
> },
>
>
> I can all but gaurantee you that when the rest of the people on this list
> wake up in the morning, you''ll get a slew of helpful comments
about
> "closures" and scope issues.
>
> This is by far the most common question/problem we see here :-)
>
> Welcome to OO javascript.
>
>
> On 5/24/06, Kev''n
<falstaff-dxFKlcbaSNapwFb5G8XvHQ@public.gmane.org> wrote:
> >
> > I think I may be missing a fundamental concept I hope someone can help
> > me with. I have functions that won''t call other functions.
Particularly
> > after an Ajax request.
> >
> > In the following object the ajax request onComplete calls the
> > ajaxFetched function successfully. ajaxFetched tries to call function2
> > but function2 does not execute.
> >
> > Any ideas or workarounds?
> >
> > thanks
> > Kevin
> >
> >
> > o = Class.create();
> > o.prototype = {
> > initialize: function() {
> > this.fetchAjaxData();
> > },
> >
> > fetchAjaxData: function(){
> > url="ajax/minimap.php";
> > pars="x=1&y=1";
> >
> > var myAjax= new Ajax.Request(
> > url,
> > {
> > method: ''get'',
> > parameters: pars,
> > onComplete: this.ajaxFetched
> > });
> > },
> >
> > ajaxFetched: function(){
> > alert(''ajaxFetched called'');
> > this.function2();
> > },
> >
> > function2: function(){
> > alert(''function2 called'');
> > }
> > }
> >
> > Event.observe(window, ''load'', function(){var O = new
o();});
> >
> > _______________________________________________
> > Rails-spinoffs mailing list
> > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
> >
>
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
>
>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Andrew, this won''t actually work for the OP''s problem. He''s not having an issue getting "this.ajaxFetched" to be called properly. It''s that once IN ajaxFetched, the call to "this.function2()" was looking at the wrong "this"... so your code would essentially have the same issue since you''re also not "applying" the outer object instance to the ajaxFetched function. On 5/25/06, Martinez, Andrew <Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:> > Sigh....didn''t finish editing the code. Changed the this.ajaxFetched call > to my functor ...functor. > > Same way to do the same thing, but uglier. > > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: functor > } > ); > > > Just be careful not to change curThis and arg1/arg2 if they are variables. > When the function is called they will have the value of their last known > state before the creating function ends (binding uhh quirks). > > By the same token you can also specify static information this way. > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2, > ''staticData'')} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: functor > } > ); > > > -Andrew Martinez > > -----Original Message----- > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Martinez, > Andrew > Sent: Thursday, May 25, 2006 9:13 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: RE: [Rails-spinoffs] functions calling functions > > Same way to do the same thing, but uglier. > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: this.ajaxFetched > } > ); > > > Just be careful not to change curThis and arg1/arg2 if they are variables. > When the function is called they will have the value of their last known > state before the creating function ends (binding uhh quirks). > > By the same token you can also specify static information this way. > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2, > ''staticData'')} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: this.ajaxFetched > } > ); > > > > -Andrew Martinez > > -----Original Message----- > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jeremy Kitchen > Sent: Thursday, May 25, 2006 3:25 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails-spinoffs] functions calling functions > > << File: ATT1071742.dat >> << File: ATT1071743.txt >> On Wednesday 24 May > 2006 23:02, Maninder, Singh wrote: > > Ryan, > > > > What''s the non prototyp-ish way to handle this? > > > > What if we don''t want to use bind()? > > it really depends. If the "this.function2" function doesn''t rely on state > within the object, just turn it into a ''class method'' and call it > directly. > If it does, then you''ll have to use bind. > > bind is really just a fancy wrapper around apply(), so you can do it > manually, > but I find bind to be much more handy :) > > -Jeremy > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
? No it wouldn''t. I have used this in quite a few places. It statically
binds to the object reference of this. By doing var someVar = this, you take the
binding away from ''this'' problems.
Try it for yourself with real code.
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]On
Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 9:44 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
Andrew, this won''t actually work for the OP''s problem.
He''s not having an issue getting "this.ajaxFetched" to be
called properly. It''s that once IN ajaxFetched, the call to
"this.function2()" was looking at the wrong "this"... so
your code would essentially have the same issue since you''re also not
"applying" the outer object instance to the ajaxFetched function.
On 5/25/06, Martinez, Andrew <
Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:
Sigh....didn''t finish editing the code. Changed the this.ajaxFetched
call to my functor ...functor.
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched (arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Martinez, Andrew
Sent: Thursday, May 25, 2006 9:13 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] functions calling functions
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Jeremy Kitchen
Sent: Thursday, May 25, 2006 3:25 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
<< File: ATT1071742.dat >> << File: ATT1071743.txt >> On
Wednesday 24 May 2006 23:02, Maninder, Singh wrote:> Ryan,
>
> What''s the non prototyp-ish way to handle this?
>
> What if we don''t want to use bind()?
it really depends. If the " this.function2" function doesn''t
rely on state
within the object, just turn it into a ''class method'' and call
it directly.
If it does, then you''ll have to use bind.
bind is really just a fancy wrapper around apply(), so you can do it manually,
but I find bind to be much more handy :)
-Jeremy
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
So you''ve verified in real code using the OP''s class that once
in ajaxFetch,
the call to "this.function2()" works using your closure?
Your closure would work if you were just concerned with getting data into
the function, that''s where this has come up before... but once in the
function if you want "this" to be the outer object, a simple closure
isn''t
the answer, you need to use apply.
Now, if you had defined your closure as such, it would work...
var functor = function(arg1, arg2){ curThis.ajaxFetched(arg1, arg2,
''staticData'').bind(curThis); };
Try it your way with the original poster''s class, if you''re
right, then I''m
surprised, but I''ll admit defeat...
On 5/25/06, Martinez, Andrew
<Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org>
wrote:>
> ? No it wouldn''t. I have used this in quite a few places. It
statically
> binds to the object reference of this. By doing var someVar = this, you
take
> the binding away from ''this'' problems.
>
>
>
> Try it for yourself with real code.
>
>
>
> -Andrew Martinez
>
>
>
> -----Original Message-----
> *From:*
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
>
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]*On
Behalf Of *Ryan Gahl
> *Sent:* Thursday, May 25, 2006 9:44 AM
> *To:* rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> *Subject:* Re: [Rails-spinoffs] functions calling functions
>
>
>
> Andrew, this won''t actually work for the OP''s problem.
He''s not having an
> issue getting "this.ajaxFetched" to be called properly.
It''s that once IN
> ajaxFetched, the call to "this.function2()" was looking at the
wrong
> "this"... so your code would essentially have the same issue
since you''re
> also not "applying" the outer object instance to the ajaxFetched
function.
>
> On 5/25/06, *Martinez, Andrew*
<Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:
>
> Sigh....didn''t finish editing the code. Changed the
this.ajaxFetched call
> to my functor ...functor.
>
> Same way to do the same thing, but uglier.
>
>
> var curThis = this;
> var functor = function(arg1, arg2){curThis.ajaxFetched (arg1, arg2)}
> var myAjax= new Ajax.Request(
> url,
> {
> method: ''get'',
> parameters: pars,
> onComplete: functor
> }
> );
>
>
> Just be careful not to change curThis and arg1/arg2 if they are variables.
> When the function is called they will have the value of their last known
> state before the creating function ends (binding uhh quirks).
>
> By the same token you can also specify static information this way.
>
> var curThis = this;
> var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
> ''staticData'')}
> var myAjax= new Ajax.Request(
> url,
> {
> method: ''get'',
> parameters: pars,
> onComplete: functor
> }
> );
>
>
> -Andrew Martinez
>
> -----Original Message-----
> From:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
> rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Martinez,
> Andrew
> Sent: Thursday, May 25, 2006 9:13 AM
> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: RE: [Rails-spinoffs] functions calling functions
>
> Same way to do the same thing, but uglier.
> var curThis = this;
> var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)}
> var myAjax= new Ajax.Request(
> url,
> {
> method: ''get'',
> parameters: pars,
> onComplete: this.ajaxFetched
> }
> );
>
>
> Just be careful not to change curThis and arg1/arg2 if they are variables.
> When the function is called they will have the value of their last known
> state before the creating function ends (binding uhh quirks).
>
> By the same token you can also specify static information this way.
>
> var curThis = this;
> var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
> ''staticData'')}
> var myAjax= new Ajax.Request(
> url,
> {
> method: ''get'',
> parameters: pars,
> onComplete: this.ajaxFetched
> }
> );
>
>
>
> -Andrew Martinez
>
> -----Original Message-----
> From:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
> rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of Jeremy Kitchen
> Sent: Thursday, May 25, 2006 3:25 AM
> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: Re: [Rails-spinoffs] functions calling functions
>
> << File: ATT1071742.dat >> << File: ATT1071743.txt
>> On Wednesday 24 May
> 2006 23:02, Maninder, Singh wrote:
> > Ryan,
> >
> > What''s the non prototyp-ish way to handle this?
> >
> > What if we don''t want to use bind()?
>
> it really depends. If the " this.function2" function
doesn''t rely on
> state
> within the object, just turn it into a ''class method'' and
call it
> directly.
> If it does, then you''ll have to use bind.
>
> bind is really just a fancy wrapper around apply(), so you can do it
> manually,
> but I find bind to be much more handy :)
>
> -Jeremy
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
>
>
> _______________________________________________
> Rails-spinoffs mailing list
> Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
>
>
>
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Done and done. I didn''t even include prototype or scriptaculous.
Old-school window.onload and alerts.
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]On
Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 12:39 PM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
So you''ve verified in real code using the OP''s class that once
in ajaxFetch, the call to "this.function2()" works using your closure?
Your closure would work if you were just concerned with getting data into the
function, that''s where this has come up before... but once in the
function if you want "this" to be the outer object, a simple closure
isn''t the answer, you need to use apply.
Now, if you had defined your closure as such, it would work...
var functor = function(arg1, arg2){ curThis.ajaxFetched(arg1, arg2,
''staticData'').bind(curThis); };
Try it your way with the original poster''s class, if you''re
right, then I''m surprised, but I''ll admit defeat...
On 5/25/06, Martinez, Andrew <
Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:
? No it wouldn''t. I have used this in quite a few places. It statically
binds to the object reference of this. By doing var someVar = this, you take the
binding away from ''this'' problems.
Try it for yourself with real code.
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 9:44 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
Andrew, this won''t actually work for the OP''s problem.
He''s not having an issue getting "this.ajaxFetched" to be
called properly. It''s that once IN ajaxFetched, the call to
"this.function2()" was looking at the wrong "this"... so
your code would essentially have the same issue since you''re also not
"applying" the outer object instance to the ajaxFetched function.
On 5/25/06, Martinez, Andrew <
Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:
Sigh....didn''t finish editing the code. Changed the this.ajaxFetched
call to my functor ...functor.
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched (arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Martinez, Andrew
Sent: Thursday, May 25, 2006 9:13 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] functions calling functions
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Jeremy Kitchen
Sent: Thursday, May 25, 2006 3:25 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
<< File: ATT1071742.dat >> << File: ATT1071743.txt >> On
Wednesday 24 May 2006 23:02, Maninder, Singh wrote:> Ryan,
>
> What''s the non prototyp-ish way to handle this?
>
> What if we don''t want to use bind()?
it really depends. If the " this.function2" function doesn''t
rely on state
within the object, just turn it into a ''class method'' and call
it directly.
If it does, then you''ll have to use bind.
bind is really just a fancy wrapper around apply(), so you can do it manually,
but I find bind to be much more handy :)
-Jeremy
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Ok, yep, you were right. I guess I wasn''t thinking, but seriously... why not just use bind()? I mean, if you''re using prototype already (which the OP clearly is), it''s much cleaner than putting those closures all over the place. Hmm, not sure why I wasn''t seeing it working in my head, but it looked conceptually the same to me as what Kev''n initially posted. Guess it''s just because I had bind() tunnel vision as I''ve always just used that to overcome the callback scope issue... So I apologize, Andrew. Either way will work, guess it''s just a matter of preference. On 5/25/06, Martinez, Andrew <Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:> > > > > > Done and done. I didn''t even include prototype or scriptaculous. > Old-school window.onload and alerts. > > > > -Andrew Martinez > > > > -----Original Message----- > *From:* rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]* On Behalf Of *Ryan Gahl > *Sent:* Thursday, May 25, 2006 12:39 PM > *To:* rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > *Subject:* Re: [Rails-spinoffs] functions calling functions > > > > So you''ve verified in real code using the OP''s class that once in > ajaxFetch, the call to "this.function2()" works using your closure? > > Your closure would work if you were just concerned with getting data into > the function, that''s where this has come up before... but once in the > function if you want "this" to be the outer object, a simple closure isn''t > the answer, you need to use apply. > > Now, if you had defined your closure as such, it would work... > > var functor = function(arg1, arg2){ curThis.ajaxFetched(arg1, arg2, > ''staticData'').bind(curThis); }; > > Try it your way with the original poster''s class, if you''re right, then > I''m surprised, but I''ll admit defeat... > > > On 5/25/06, *Martinez, Andrew* <Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote: > > ? No it wouldn''t. I have used this in quite a few places. It statically > binds to the object reference of this. By doing var someVar = this, you take > the binding away from ''this'' problems. > > > > Try it for yourself with real code. > > > > -Andrew Martinez > > > > -----Original Message----- > *From:* rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]* On Behalf Of *Ryan Gahl > *Sent:* Thursday, May 25, 2006 9:44 AM > *To:* rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > *Subject:* Re: [Rails-spinoffs] functions calling functions > > > > Andrew, this won''t actually work for the OP''s problem. He''s not having an > issue getting "this.ajaxFetched" to be called properly. It''s that once IN > ajaxFetched, the call to "this.function2()" was looking at the wrong > "this"... so your code would essentially have the same issue since you''re > also not "applying" the outer object instance to the ajaxFetched function. > > On 5/25/06, *Martinez, Andrew * <Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote: > > Sigh....didn''t finish editing the code. Changed the this.ajaxFetched call > to my functor ...functor. > > Same way to do the same thing, but uglier. > > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched (arg1, arg2)} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: functor > } > ); > > > Just be careful not to change curThis and arg1/arg2 if they are variables. > When the function is called they will have the value of their last known > state before the creating function ends (binding uhh quirks). > > By the same token you can also specify static information this way. > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2, > ''staticData'')} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: functor > } > ); > > > -Andrew Martinez > > -----Original Message----- > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Martinez, > Andrew > Sent: Thursday, May 25, 2006 9:13 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: RE: [Rails-spinoffs] functions calling functions > > Same way to do the same thing, but uglier. > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: this.ajaxFetched > } > ); > > > Just be careful not to change curThis and arg1/arg2 if they are variables. > When the function is called they will have the value of their last known > state before the creating function ends (binding uhh quirks). > > By the same token you can also specify static information this way. > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2, > ''staticData'')} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: this.ajaxFetched > } > ); > > > > -Andrew Martinez > > -----Original Message----- > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jeremy Kitchen > Sent: Thursday, May 25, 2006 3:25 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails-spinoffs] functions calling functions > > << File: ATT1071742.dat >> << File: ATT1071743.txt >> On Wednesday 24 May > 2006 23:02, Maninder, Singh wrote: > > Ryan, > > > > What''s the non prototyp-ish way to handle this? > > > > What if we don''t want to use bind()? > > it really depends. If the " this.function2" function doesn''t rely on > state > within the object, just turn it into a ''class method'' and call it > directly. > If it does, then you''ll have to use bind. > > bind is really just a fancy wrapper around apply(), so you can do it > manually, > but I find bind to be much more handy :) > > -Jeremy > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Yea, no problem.
Bind is exactly the same thing as what I showed. It just makes the syntax look
nicer. Before prototype, most people would just make a function that works like
bind or if they were creating a slim stand alone script they would use what I
suggested. I only brought it up because the original question asker wanted to
know the non-prototype way of doing it. I just obliged him.
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]On
Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 3:06 PM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
Ok, yep, you were right. I guess I wasn''t thinking, but seriously...
why not just use bind()?
I mean, if you''re using prototype already (which the OP clearly is),
it''s much cleaner than putting those closures all over the place.
Hmm, not sure why I wasn''t seeing it working in my head, but it looked
conceptually the same to me as what Kev''n initially posted. Guess
it''s just because I had bind() tunnel vision as I''ve always
just used that to overcome the callback scope issue...
So I apologize, Andrew. Either way will work, guess it''s just a matter
of preference.
On 5/25/06, Martinez, Andrew <
<mailto:Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org>
Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:
Done and done. I didn''t even include prototype or scriptaculous.
Old-school window.onload and alerts.
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 12:39 PM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
So you''ve verified in real code using the OP''s class that once
in ajaxFetch, the call to "this.function2()" works using your closure?
Your closure would work if you were just concerned with getting data into the
function, that''s where this has come up before... but once in the
function if you want "this" to be the outer object, a simple closure
isn''t the answer, you need to use apply.
Now, if you had defined your closure as such, it would work...
var functor = function(arg1, arg2){ curThis.ajaxFetched(arg1, arg2,
''staticData'').bind(curThis); };
Try it your way with the original poster''s class, if you''re
right, then I''m surprised, but I''ll admit defeat...
On 5/25/06, Martinez, Andrew <
Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:
? No it wouldn''t. I have used this in quite a few places. It statically
binds to the object reference of this. By doing var someVar = this, you take the
binding away from ''this'' problems.
Try it for yourself with real code.
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 9:44 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
Andrew, this won''t actually work for the OP''s problem.
He''s not having an issue getting "this.ajaxFetched" to be
called properly. It''s that once IN ajaxFetched, the call to
"this.function2()" was looking at the wrong "this"... so
your code would essentially have the same issue since you''re also not
"applying" the outer object instance to the ajaxFetched function.
On 5/25/06, Martinez, Andrew <
Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:
Sigh....didn''t finish editing the code. Changed the this.ajaxFetched
call to my functor ...functor.
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched (arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: functor
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Martinez, Andrew
Sent: Thursday, May 25, 2006 9:13 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: RE: [Rails-spinoffs] functions calling functions
Same way to do the same thing, but uglier.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
Just be careful not to change curThis and arg1/arg2 if they are variables. When
the function is called they will have the value of their last known state before
the creating function ends (binding uhh quirks).
By the same token you can also specify static information this way.
var curThis = this;
var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2,
''staticData'')}
var myAjax= new Ajax.Request(
url,
{
method: ''get'',
parameters: pars,
onComplete: this.ajaxFetched
}
);
-Andrew Martinez
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On
Behalf Of Jeremy Kitchen
Sent: Thursday, May 25, 2006 3:25 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: Re: [Rails-spinoffs] functions calling functions
<< File: ATT1071742.dat >> << File: ATT1071743.txt >> On
Wednesday 24 May 2006 23:02, Maninder, Singh wrote:> Ryan,
>
> What''s the non prototyp-ish way to handle this?
>
> What if we don''t want to use bind()?
it really depends. If the " this.function2" function doesn''t
rely on state
within the object, just turn it into a ''class method'' and call
it directly.
If it does, then you''ll have to use bind.
bind is really just a fancy wrapper around apply(), so you can do it manually,
but I find bind to be much more handy :)
-Jeremy
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
See, for some reason I got it in my head that bind()''s use of "apply" was the important piece, and that it was needed on TOP of the closure. I had never tried just using a closure for this purpose (although I use them alot for other reasons)... thanks for the education. On 5/25/06, Martinez, Andrew <Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote:> > Yea, no problem. > > > > Bind is exactly the same thing as what I showed. It just makes the syntax > look nicer. Before prototype, most people would just make a function that > works like bind or if they were creating a slim stand alone script they > would use what I suggested. I only brought it up because the original > question asker wanted to know the non-prototype way of doing it. I just > obliged him. > > > > -Andrew Martinez > > > > -----Original Message----- > *From:* rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]*On Behalf Of *Ryan Gahl > *Sent:* Thursday, May 25, 2006 3:06 PM > *To:* rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > *Subject:* Re: [Rails-spinoffs] functions calling functions > > > > Ok, yep, you were right. I guess I wasn''t thinking, but seriously... why > not just use bind()? > > I mean, if you''re using prototype already (which the OP clearly is), it''s > much cleaner than putting those closures all over the place. > > Hmm, not sure why I wasn''t seeing it working in my head, but it looked > conceptually the same to me as what Kev''n initially posted. Guess it''s just > because I had bind() tunnel vision as I''ve always just used that to overcome > the callback scope issue... > > So I apologize, Andrew. Either way will work, guess it''s just a matter of > preference. > > On 5/25/06, *Martinez, Andrew* < Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote: > > > > > > Done and done. I didn''t even include prototype or scriptaculous. > Old-school window.onload and alerts. > > > > -Andrew Martinez > > > > -----Original Message----- > *From:* rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]* On Behalf Of *Ryan Gahl > > *Sent:* Thursday, May 25, 2006 12:39 PM > *To:* rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > *Subject:* Re: [Rails-spinoffs] functions calling functions > > > > So you''ve verified in real code using the OP''s class that once in > ajaxFetch, the call to "this.function2()" works using your closure? > > Your closure would work if you were just concerned with getting data into > the function, that''s where this has come up before... but once in the > function if you want "this" to be the outer object, a simple closure isn''t > the answer, you need to use apply. > > Now, if you had defined your closure as such, it would work... > > var functor = function(arg1, arg2){ curThis.ajaxFetched(arg1, arg2, > ''staticData'').bind(curThis); }; > > Try it your way with the original poster''s class, if you''re right, then > I''m surprised, but I''ll admit defeat... > > On 5/25/06, *Martinez, Andrew* <Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote: > > ? No it wouldn''t. I have used this in quite a few places. It statically > binds to the object reference of this. By doing var someVar = this, you take > the binding away from ''this'' problems. > > > > Try it for yourself with real code. > > > > -Andrew Martinez > > > > -----Original Message----- > *From:* rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]* On Behalf Of *Ryan Gahl > *Sent:* Thursday, May 25, 2006 9:44 AM > *To:* rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > *Subject:* Re: [Rails-spinoffs] functions calling functions > > > > Andrew, this won''t actually work for the OP''s problem. He''s not having an > issue getting "this.ajaxFetched" to be called properly. It''s that once IN > ajaxFetched, the call to "this.function2()" was looking at the wrong > "this"... so your code would essentially have the same issue since you''re > also not "applying" the outer object instance to the ajaxFetched function. > > On 5/25/06, *Martinez, Andrew *<Andrew.Martinez-9sMr025MA47QT0dZR+AlfA@public.gmane.org> wrote: > > Sigh....didn''t finish editing the code. Changed the this.ajaxFetched call > to my functor ...functor. > > Same way to do the same thing, but uglier. > > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched (arg1, arg2)} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: functor > } > ); > > > Just be careful not to change curThis and arg1/arg2 if they are variables. > When the function is called they will have the value of their last known > state before the creating function ends (binding uhh quirks). > > By the same token you can also specify static information this way. > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2, > ''staticData'')} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: functor > } > ); > > > -Andrew Martinez > > -----Original Message----- > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Martinez, > Andrew > Sent: Thursday, May 25, 2006 9:13 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: RE: [Rails-spinoffs] functions calling functions > > Same way to do the same thing, but uglier. > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2)} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: this.ajaxFetched > } > ); > > > Just be careful not to change curThis and arg1/arg2 if they are variables. > When the function is called they will have the value of their last known > state before the creating function ends (binding uhh quirks). > > By the same token you can also specify static information this way. > > var curThis = this; > var functor = function(arg1, arg2){curThis.ajaxFetched(arg1, arg2, > ''staticData'')} > var myAjax= new Ajax.Request( > url, > { > method: ''get'', > parameters: pars, > onComplete: this.ajaxFetched > } > ); > > > > -Andrew Martinez > > -----Original Message----- > From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jeremy Kitchen > Sent: Thursday, May 25, 2006 3:25 AM > To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails-spinoffs] functions calling functions > > << File: ATT1071742.dat >> << File: ATT1071743.txt >> On Wednesday 24 May > 2006 23:02, Maninder, Singh wrote: > > Ryan, > > > > What''s the non prototyp-ish way to handle this? > > > > What if we don''t want to use bind()? > > it really depends. If the " this.function2" function doesn''t rely on > state > within the object, just turn it into a ''class method'' and call it > directly. > If it does, then you''ll have to use bind. > > bind is really just a fancy wrapper around apply(), so you can do it > manually, > but I find bind to be much more handy :) > > -Jeremy > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > > > > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs