> ... 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 <[EMAIL PROTECTED]> wrote: > Thank you! > I'm still confused about the syntax, but this saved my problem. > > - D. > > On Mar 24, 3:59 am, kangax <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---