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=""> 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 <[EMAIL PROTECTED]> 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=""> 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
_______________________________________________ Rails-spinoffs mailing list [email protected] http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
