Hello! I''m wondering how solve the following problem: (coded directly from my head, so it might have errors, but I think you''ll get my idea) var SuperClass = Class.create(); SuperClass.prototype = { initialize : function() { }, getSomeAjaxInfo : function() { new Ajax.Request( ''somexml.xml'', { method:''get'', onSucces: <<I want to call onAjaxInfroReturned from class SuperClass here>> }); }, onAjaxInfoReturned : function() { } }; How can I call the method onAjaxInfoReturned from the ajax request I made in some method from class SuperClass? Thank you for your explanations. D. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
To be more clear I''ve prepaired next example and tested it: var SuperClass = Class.create(); SuperClass.prototype = { test : "Test data is ", initialize : function(){ this.doRequest(); }, doRequest : function() { new Ajax.Request(''somedata.php'', { method: ''get'', onSuccess : this.callback() } ); }, callback : function(data) { alert(this.test+":"+data); } }; var sc = new SuperClass(); I want the callback function say "Test data is [data retreived from AJAX call]". In case I have "onSuccess : this.callback()" this.test is NULL, in case I use "onSuccess : this.callback" data is NULL. How to solve this problem? Thank you. D. On Mar 22, 10:10 pm, dj <denis.justi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello! > > I''m wondering how solve the following problem: > (coded directly from my head, so it might have errors, but I think > you''ll get my idea) > > var SuperClass = Class.create(); > SuperClass.prototype = { > initialize : function() { > > }, > > getSomeAjaxInfo : function() { > new Ajax.Request( ''somexml.xml'', { method:''get'', onSucces: <<I want > to call onAjaxInfroReturned from class SuperClass here>> });}, > > onAjaxInfoReturned : function() { > > } > }; > > How can I call the method onAjaxInfoReturned from the ajax request I > made in some method from class SuperClass? > > Thank you for your explanations. > D.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
You need to bind this.callback(); because its loosing the proper scope of "this". http://www.prototypejs.org/api/function/bind this.callback.bind(this) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ahhh thank you for your answer. It really saved me. Where can I read some detailed documentation regarding "this" keyword and how to use it correctly with prototypejs? I have one more question regarding this (I''ll keep the same example): var SuperClass = Class.create(); SuperClass.prototype = { test : "Test data is ", initialize : function(){ this.doRequest(); }, doRequest : function() { new Ajax.Request(''somedata.php'', { method: ''get'', onSuccess : this.callback.bind(this) <<== **here** } ); }, callback : function(data) { alert(this.test+":"+data); } }; if I want to have instead of this.callback.bind(this) implemented function which calls callback (only when needed) for example: var SuperClass = Class.create(); SuperClass.prototype = { test : "Test data is ", initialize : function(){ this.doRequest(); }, doRequest : function() { new Ajax.Request(''somedata.php'', { method: ''get'', onSuccess : function(data) { do some processing; . . . ** this.callback.bind(this); how to call callback from here ** } } ); }, callback : function(data) { alert(this.test+":"+data); } }; Thank you. D. On Mar 23, 12:43 am, jdalton <John.David.Dal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You need to bind this.callback(); > because its loosing the proper scope of "this".http://www.prototypejs.org/api/function/bind > > this.callback.bind(this)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
You need to "bind" the actual callback function (since it will be executed in a global context otherwise): ... onSuccess: function(data) { // do some processing... this.callback(); // <= "bind" ensured that "this" refers to your instance }.bind(this) ... - kangax On Mar 23, 7:20 pm, dj <denis.justi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ahhh thank you for your answer. It really saved me. > Where can I read some detailed documentation regarding "this" keyword > and how to use it correctly with prototypejs? > > I have one more question regarding this (I''ll keep the same example): > > var SuperClass = Class.create(); > SuperClass.prototype = { > test : "Test data is ", > initialize : function(){ > this.doRequest(); > }, > doRequest : function() { > new Ajax.Request(''somedata.php'', { > method: ''get'', > onSuccess : > this.callback.bind(this) <<== **here** > } ); > }, > callback : function(data) { > alert(this.test+":"+data); > } > > }; > > if I want to have instead of this.callback.bind(this) implemented > function which calls callback (only when needed) for example: > > var SuperClass = Class.create(); > SuperClass.prototype = { > test : "Test data is ", > initialize : function(){ > this.doRequest(); > }, > doRequest : function() { > new Ajax.Request(''somedata.php'', { > method: ''get'', > onSuccess : > function(data) { do some processing; . . . ** > this.callback.bind(this); how to call callback from here ** } > } ); > }, > callback : function(data) { > alert(this.test+":"+data); > } > > }; > > Thank you. > D. > > On Mar 23, 12:43 am, jdalton <John.David.Dal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > You need to bind this.callback(); > > because its loosing the proper scope of "this".http://www.prototypejs.org/api/function/bind > > > this.callback.bind(this)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you! I''m still confused about the syntax, but this saved my problem. - D. On Mar 24, 3:59 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You need to "bind" the actual callback function (since it will be > executed in a global context otherwise): > ... > onSuccess: function(data) { > // do some processing... > this.callback(); // <= "bind" ensured that "this" refers to your > instance}.bind(this) > > ... > > - kangax > > On Mar 23, 7:20 pm, dj <denis.justi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Ahhh thank you for your answer. It really saved me. > > Where can I read some detailed documentation regarding "this" keyword > > and how to use it correctly with prototypejs? > > > I have one more question regarding this (I''ll keep the same example): > > > var SuperClass = Class.create(); > > SuperClass.prototype = { > > test : "Test data is ", > > initialize : function(){ > > this.doRequest(); > > }, > > doRequest : function() { > > new Ajax.Request(''somedata.php'', { > > method: ''get'', > > onSuccess : > > this.callback.bind(this) <<== **here** > > } ); > > }, > > callback : function(data) { > > alert(this.test+":"+data); > > } > > > }; > > > if I want to have instead of this.callback.bind(this) implemented > > function which calls callback (only when needed) for example: > > > var SuperClass = Class.create(); > > SuperClass.prototype = { > > test : "Test data is ", > > initialize : function(){ > > this.doRequest(); > > }, > > doRequest : function() { > > new Ajax.Request(''somedata.php'', { > > method: ''get'', > > onSuccess : > > function(data) { do some processing; . . . ** > > this.callback.bind(this); how to call callback from here ** } > > } ); > > }, > > callback : function(data) { > > alert(this.test+":"+data); > > } > > > }; > > > Thank you. > > D. > > > On Mar 23, 12:43 am, jdalton <John.David.Dal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > You need to bind this.callback(); > > > because its loosing the proper scope of "this".http://www.prototypejs.org/api/function/bind > > > > this.callback.bind(this)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
> ... I''m still confused about the syntax...Fair ''nuff. :-) There are several potentially-confusing things here. I''ll break it down some: 1. Event handlers (like onSuccess) are triggered with the default "this" object; the default "this" object is the global object, which in browser implementations is the window object. You might think that doesn''t make sense if you say "onSuccess: this.handler" -- surely "this" should be, well, "this"? -- but all you''re doing when you say "onSuccess: this.handler" is setting the "onSuccess" option to the *function* referenced by the "handler" property of "this" object; there''s nothing in that function instance that references the instance you retrieved it from. There''s more info here: http://blog.niftysnippets.org/2008/03/mythical-methods.html (disclaimer: it''s my blog) but summarizing quite a bit, JavaScript doesn''t really have (or need) "methods", there are just functions that can be referenced via properties on objects. We talk about "methods" because that''s a convenient way to think of them, but they''re just function references. 2. Functions are objects just like everything else in JavaScript; "myFunction" refers to the function object instance, whereas "myFunction()" (with the parentheses) *calls* the function. Since functions are objects, they can have properties (and those properties can refer to functions; e.g., "methods"). 3. There''s a way to call functions and control what the "this" reference should be. Details in the blog post, but basically it''s the "apply" or "call" property on the function instance. So if I have a function like: function myFunction() { ... } Then I can call it like this: myFunction(); ...but then it will get the default "this" object. I can call it and tell it what "this" should be like this: myFunction.apply(thisObject); So far, none of the above is specific to Prototype; it''s all just JavaScript. 4. It''s so common to want to have a function reference that calls a "method" of a specific object instance that Prototype adds a method to Function objects called bind() that makes it easy to bind a function to an object instance: bind() returns a new function that, when called, will call the function you''re binding using its "apply" method to make sure the object instance is "this" while the function is running. So: boundFunction = unboundFunction.bind(objectToBindTo); In essence (and ignoring some implementation details), the above could be replaced with: boundFunction = function() { return unboundFunction.apply(objectToBindTo, arguments); }; ...but see the bind() document (or even implementation) for details. The net effect of all of the above is that if you have a handler function "handler" and you''re writing code where "this" currently refers to the object you want the handler to act upon, then specify onSuccess like this: this.handler.bind(this) ...which means "take the function reference from this.handler, bind ''this'' to it in a wrapper function, and use that." (Note that if you''re using the handler as an event handler -- for instance, on a button click or something -- you want to use bindAsEventListener() instead, to work around/mitigate an Internet Explorer bug.) Hope this helps, -- T.J. Crowder tj / crowder software / com On Mar 24, 1:45 pm, dj <denis.justi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thank you! > I''m still confused about the syntax, but this saved my problem. > > - D. > > On Mar 24, 3:59 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > You need to "bind" the actual callback function (since it will be > > executed in a global context otherwise): > > ... > > onSuccess: function(data) { > > // do some processing... > > this.callback(); // <= "bind" ensured that "this" refers to your > > instance}.bind(this) > > > ... > > > - kangax > > > On Mar 23, 7:20 pm, dj <denis.justi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Ahhh thank you for your answer. It really saved me. > > > Where can I read some detailed documentation regarding "this" keyword > > > and how to use it correctly with prototypejs? > > > > I have one more question regarding this (I''ll keep the same example): > > > > var SuperClass = Class.create(); > > > SuperClass.prototype = { > > > test : "Test data is ", > > > initialize : function(){ > > > this.doRequest(); > > > }, > > > doRequest : function() { > > > new Ajax.Request(''somedata.php'', { > > > method: ''get'', > > > onSuccess : > > > this.callback.bind(this) <<== **here** > > > } ); > > > }, > > > callback : function(data) { > > > alert(this.test+":"+data); > > > } > > > > }; > > > > if I want to have instead of this.callback.bind(this) implemented > > > function which calls callback (only when needed) for example: > > > > var SuperClass = Class.create(); > > > SuperClass.prototype = { > > > test : "Test data is ", > > > initialize : function(){ > > > this.doRequest(); > > > }, > > > doRequest : function() { > > > new Ajax.Request(''somedata.php'', { > > > method: ''get'', > > > onSuccess : > > > function(data) { do some processing; . . . ** > > > this.callback.bind(this); how to call callback from here ** } > > > } ); > > > }, > > > callback : function(data) { > > > alert(this.test+":"+data); > > > } > > > > }; > > > > Thank you. > > > D. > > > > On Mar 23, 12:43 am, jdalton <John.David.Dal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > You need to bind this.callback(); > > > > because its loosing the proper scope of "this".http://www.prototypejs.org/api/function/bind > > > > > this.callback.bind(this)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 24, 11:30 pm, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 1. Event handlers (like onSuccess) are triggered with the default > "this" object; the default "this" object is the global object, which > in browser implementations is the window object.Apologies for the follow-up post, but I realized when reading another thread in this forum [1] that the above could be a bit misleading if you''re using Prototype''s Event.observe method [2] or the DOM standard addEventListener function [3], because both of those ensure that "this" is set to the element being observed, not the global object, during the call to the handler. The old DOM0 way of hooking up events (using an onclick attribute in your HTML) and Internet Explorer''s attachEvent function [4] both use the global object as "this". (Hence Prototype abstracting away the differences for us with Event.observe, making IE behave as though it supported the standard method.) Sorry for the confusion! [1] http://groups.google.com/group/rubyonrails-spinoffs/browse_thread/thread/b0ea48d3b2847c96 [2] http://www.prototypejs.org/api/event/observe [3] http://developer.mozilla.org/en/docs/DOM:element.addEventListener [4] http://msdn2.microsoft.com/en-us/library/ms536343(VS.85).aspx -- T.J. Crowder tj / crowder software / com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---