Thanks a lot T.J. I will read your answer carefully later on, but it looks very complete and clear!!!!
I really appreciate your help. --- Omar Adobati On Wed, Mar 24, 2010 at 15:08, T.J. Crowder <[email protected]> wrote: > Hi Omar, > > In the general case, you can reference a function simply by using its > name whenever it's "in scope," which is to say, if you could call it. > So: > > function foo() { > alert("Hi from foo!"); > } > function bar(func) { > func(); > } > foo(); > bar(foo); > > ...results in two alerts, the first from `foo` directly, the second > from calling `bar` and giving it a reference to `foo` as an argument, > which it then calls. (In this example, it happens that `bar` could > also have just called `foo` directly, but ignore that.) Note that when > we called `bar`, *we* didn't call `foo` (there are no parentheses > after it), we passed a *reference* to it into `bar` (which then called > it -- but it didn't have to, it could have remembered it to call later > for some reason). > > A function is in scope if it's defined in the current function or any > containing function (with top level global space effectively being a > function). So: > > function charlie() { > bar(foo); > function foo() { > alert("Hi from foo!"); > } > } > function bar(func) { > func(); > } > charlie(); // <= That's fine, shows alert > foo(); // <= syntax error, `foo` is undefined > bar(foo); // <= syntax error, `foo` is undefined > > In the above, `foo` is defined within `charlie` but not outside of it. > If we defined other functions within `charlie`, they'd also have > access to `foo`. `bar` doesn't have access to `foo`, but when > `charlie` calls `bar` it passes in the function reference to `foo` as > an argument. `bar` has access to its arguments (obviously!) and so can > call the function that way. (A function within a function is sometimes > called a "closure" because it has access to the data in the function > that surrounds it. `foo` is a closure within `charlie`.) > > In your specific situation, I'm not clear what it is you're trying to > achieve with those arguments, so I can't really help with what > function references you'd use. Also, very few of your functions > actually have names (most of them are anonymous functions bound to > object properties -- the properties have names, but the functions > don't, so you can only refer to the functions via the object > properties). I think that if you're subclassing Ajax.Request, you > wouldn't need to pass arguments at that point in the options if you're > trying to refer to the functions that your subclass has attached to > properties with those names; you'd use them directly in the subclass, > e.g.: > > initialize: function($super, url, options) { > var opts; > > // Create our options, starting with what the user supplied > opts = Object.extend({}, options); > > // ...and adding in our handlers > opts.onSuccess = this.success.bind(this); > opts.onException = this.error.bind(this); > /* ...etc... */ > > // Set some other stuff > this.setUserID(options.usr); > this.setPassword(options.pwd); > this.setType(options.type); > > // Determine the URL > var url = this.setUrl(this.getUserID(), this.getPassword(), > this.getType()); > > // Pass our updated options on to $super > $super(url, opts); > } > > ...but that's kind of a guess. (Also note that I'm using Function#bind > to ensure that when your handlers get called, they get called with the > right `this` value -- because you're using an instance, so I'm > guessing you want access to the instance data.) > > There's a lot of info and jargon above, sorry about that. These blog > posts may be useful: > > Regarding scope, there's some here: > > > http://blog.niftysnippets.org/2008/03/when-you-absolutely-positively-need-new.html > > ...and a *lot* more here, as well as more information on closures: > > http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html > > Regarding function names vs. property names: > > http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html > > And separately, the API docs for Function#bind: > > http://api.prototypejs.org/language/function.html#bind-instance_method > > HTH, > -- > T.J. Crowder > Independent Software Consultant > tj / crowder software / com > www.crowdersoftware.com > > > On Mar 24, 1:11 pm, 0m4r <[email protected]> wrote: > > Wow, thanks T.J.!! > > your answer makes a lot of sense.. but, now, how I am supposed to pass > > a function name instead of a string? > > I'm sorry if the question sounds stupid to you but I don't know how to > > achieve this target. > > > > Thanks > > > > Omar > > > > On Mar 24, 12:55 pm, "T.J. Crowder" <[email protected]> wrote: > > > > > > > > > Hi, > > > > > In this code: > > > > > function click() { > > > var opt = { > > > method: 'get', > > > onCreate: 'create', > > > onLoading: 'loading', > > > onSuccess: 'sucess', > > > onException: 'error' > > > }; > > > var login = new jsTTALogin(URL, opt); > > > > > } > > > > > ...you're providing strings instead of functions. That's causing an > > > exception, which Prototype then tries to handle via `onException` -- > > > but `onException` is *also* a string, not a function. The error > > > message in Firefox+Firebug is wrong (interestingly). It's triggered > > > from line 1540 in Prototype.js: > > > > > 1539 dispatchException: function(exception) { > > > 1540 (this.options.onException || Prototype.emptyFunction)(this, > > > exception); > > > 1541 Ajax.Responders.dispatch('onException', this, exception); > > > 1542 } > > > > > ...which resolves to: > > > > > "error"(this, exception) > > > > > ...and of course, "error" is a string, not a function. > > > > > So that's what's going wrong. It looks to me like you're trying to > > > pass the names of functions that exist in your subclass as options. > > > That's not how you reference functions, you need to provide a function > > > reference. > > > > > Off-topic: I'm a bit surprised that `initTTA` in jsTTA.js works (but > > > it does seem to), it seems to me that it's trying to call `eval` on a > > > script element, which I would expect to have...undefined results. You > > > can (and probably should) drop that `eval` and it should work just > > > fine, more here: > http://proto-scripty.wikidot.com/prototype:how-to-load-scripts-dynami... > > > > > HTH, > > > -- > > > T.J. Crowder > > > Independent Software Consultant > > > tj / crowder software / comwww.crowdersoftware.com > > > > > On Mar 24, 10:45 am, Omar Adobati <[email protected]> wrote: > > > > > > Hi T.J. > > > > I've set up a small minimum failing test here: > http://www.adobati.it/test/ > > > > <http://www.adobati.it/test/>I have the same error as described > above: > > > > > > == > > > > Prototype.emptyFunction is not a function > > > > (this.options.onException || Prototype.emptyFunction)(this, > exception); > > > > == > > > > > > Thanks, > > > > > > Omar > > > > > > On Wed, Mar 24, 2010 at 10:26, T.J. Crowder < > [email protected]> wrote: > > > > > Hi, > > > > > > > Again, why not just create a minimum failing test case? Start > here[1], > > > > > add your subclass, and if it breaks post the code. > > > > > > > [1]http://proto-scripty.wikidot.com/self-contained-test-page > > > > > > > -- T.J. > > > > > > > On Mar 23, 4:39 pm, 0m4r <[email protected]> wrote: > > > > > > fixing a typo: > > > > > > > > == > > > > > > var options = {user: 'myuser', pwd:'mypwd', type:'myType', > > > > > > onSuccess:'success', onException:'error'}; > > > > > > var url = 'http://www.url.com'; > > > > > > var myAjax = new myAjaxR(url, options) > > > > > > == > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > Groups > > > > > "Prototype & script.aculo.us" group. > > > > > To post to this group, send email to > > > > > [email protected]. > > > > > To unsubscribe from this group, send email to > > > > > [email protected]<prototype-scriptaculous%[email protected]><prototype-scriptaculou > s%[email protected] <s%[email protected]>> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/prototype-scriptaculous?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Prototype & script.aculo.us" group. > To post to this group, send email to > [email protected]. > To unsubscribe from this group, send email to > [email protected]<prototype-scriptaculous%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/prototype-scriptaculous?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
