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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 3:06 PM
To: [email protected]
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 < [EMAIL PROTECTED]> wrote:

 

 

Done and done. I didn't even include prototype or scriptaculous. Old-school window.onload and alerts.

 

-Andrew Martinez

 

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Ryan Gahl

Sent: Thursday, May 25, 2006 12:39 PM
To: [email protected]
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 <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Ryan Gahl
Sent: Thursday, May 25, 2006 9:44 AM
To: [email protected]
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 < [EMAIL PROTECTED]> 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:   [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]  On Behalf Of Martinez, Andrew
Sent:   Thursday, May 25, 2006 9:13 AM
To:     [email protected]
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:   [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]  On Behalf Of Jeremy Kitchen
Sent:   Thursday, May 25, 2006 3:25 AM
To:     [email protected]
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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs

 


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

 


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



 


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



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

Reply via email to