Search the web
Sign In
New User? Sign Up
ydn-javascript · Yahoo! User Interface Library Group
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Problem with scope on connection callback   Message List  
Reply | Forward Message #50217 of 52114 |
Re: Problem with scope on connection callback

Hi Satyam,
Thanks for this advice which I have followed and the code now works. The thing
that surprises me is the absence of 'this'!

You are right - I had adapted the module pattern model - I will think through
the implications of these approaches.

Thanks, Alex


--- In ydn-javascript@yahoogroups.com, Satyam <satyam@...> wrote:
>
> I guess the reason you are not getting any answer it because your code
> is very convoluted and without any reason to make it that way, it is
> hard to help. I am sure you are trying to apply the module pattern as
> described in:
>
> http://yuiblog.com/blog/2007/06/12/module-pattern/
>
> While this pattern is useful for libraries, where you want to have some
> properties visible others not, for application code, the final product,
> it is quite pointless. Why would you be making some properties or
> functions public if nobody is going to use them, why would you hide some
> others if nobody is looking?
>
> For such an application, it is easier and less troublesome to use
> closure to access common variables and, at the same time, keep
> everything hidden from the global scope by enclosing it in an anonymous
> function, like:
>
> YAHOO.util.Event.onDOMReady(function () {
> var divId;
>
> var whichever = function () {
>
>
> }
>
>
> });
>
> The anonymous function which is set as the listener for onDOMReady would
> contain all variables that are to be reachable by the whole application,
> in your case, divId. This property will not be visible from outside nor
> will it go into the global namespaces, it is a local variable of that
> anonymous function. Nevertheless, it is visible to all functions
> declared within that function just by name, without any 'this' or
> scoping issues, unless any of those functions have a local variable or
> argument by the same name. Thus, within whichever you can access divId
> directly and since you are not using 'this' then you don't care any
> longer what the scope might be.
>
> Satyam
>
>
> alexlebek escribió:
> >
> >
> > Hi,
> > I am having endless trouble with scoping in a connection callback! Can
> > someone explain where I am going wrong in this (truncated) code
> > extract? The problem is that the 'divId' is undefined in handleSuccess
> > . I thought that passing 'this' as the scope in the connectionCallback
> > would make the scope correct but it didn't. I have tried endless
> > permutations but obviously not the right one. This must be bread and
> > butter to someone out there...
> >
> > function MyNewPanel()
> > {
> > var AjaxObject = { // XHR response handler
> >
> > handleSuccess: function(o) {
> > // Use the JSON Utility to parse the data returned from
> > the server
> > try {
> > response = YAHOO.lang.JSON.parse(o.responseText); //
> > successful return of content
> >
> >
> > // access div element for this content
> > var elId = document.getElementById(divId); //ERROR
> > HERE *divId* undefined
> >
> > }
> > catch (x) {
> > alert("FAILURE:JSON Parse failed!");
> > return;
> > }
> > },
> >
> > handleFailure: function(o) {
> > alert("FAILURE: HTTP response failure!");
> > return;
> > },
> >
> > startRequest: function(callback) {
> > var query = 'action=' + encodeURIComponent("Load");
> > var res = YAHOO.util. Connect.asyncRequest("GET", '/rpc?'
> > + query, connectionCallback);
> > }
> >
> > };
> >
> > var connectionCallback = {
> > success: AjaxObject.handleSuccess,
> > failure: AjaxObject.handleFailure,
> > scope: this
> > }
> >
> > return {
> > divId: "MissingId",
> >
> > init: function (elPanelLabelId) {
> > this.divId = YAHOO.util.Dom.generateId(null,
> > [elPanelLabelId]); // successfully created
> > YAHOO.util.Event.onDOMReady(this.createDialog, this, true);
> > },
> >
> > createDialog: function() {
> > // Instantiate a Panel from script here
> > this.PanelObj = new YAHOO.widget.Panel(this.elPanelLabelId,
> > { width:"320px", height:"200px", visible:false, draggable:true,
> > close:false } );
> > this.PanelObj.setHeader(this.title);
> > this.PanelObj.setBody('<div id="' + this.divId +
> > '"></div>'); // successfully inserted
> >
> > blah,blah, blah
> >
> >
> > // now make connection
> > AjaxObject.startRequest(); // successful connection made
> >
> > }// end of createDialog
> >
> > }; //end of return
> >
> > }
> > // then I create one
> > var panel = new MyNewPanel();
> > panel.init('MyPanelId', 'My title'); // hang about until dom loaded
> >
> >
> >
> >
> > Thanks for any help
> > Alex
> >
> >
> > ------------------------------------------------------------------------
> >
> >
> > No virus found in this incoming message.
> > Checked by AVG - www.avg.com
> > Version: 8.5.375 / Virus Database: 270.13.2/2215 - Release Date: 07/02/09
18:06:00
> >
> >
>





Fri Jul 3, 2009 8:30 pm

alexlebek
Offline Offline
Send Email Send Email

Forward
Message #50217 of 52114 |
Expand Messages Author Sort by Date

Hi, I am having endless trouble with scoping in a connection callback! Can someone explain where I am going wrong in this (truncated) code extract? The problem...
alexlebek
Offline Send Email
Jul 3, 2009
1:59 pm

It doesn't seem a scoping problem to me, you are not using "this" to retreive your element, therefore whether your scope is right or wrong doesn't make any...
Paolo Nesti Poggi
paolonesti
Offline Send Email
Jul 3, 2009
3:19 pm

Hi Paolo, Thanks for the taking the time to look at this (I had somehow missed your reply). That is exactly what I thought - but even with the variable divId...
alexlebek
Offline Send Email
Jul 6, 2009
8:18 am

Hi, I find that, apart from the module pattern, that Satyam referrred to, much of what is needed to know about scoping in Javscript is in this article by...
Paolo Nesti Poggi
paolonesti
Offline Send Email
Jul 6, 2009
2:31 pm

I guess the reason you are not getting any answer it because your code is very convoluted and without any reason to make it that way, it is hard to help. I am...
Satyam
satyamutsa
Offline Send Email
Jul 3, 2009
6:18 pm

Hi Satyam, Thanks for this advice which I have followed and the code now works. The thing that surprises me is the absence of 'this'! You are right - I had...
alexlebek
Offline Send Email
Jul 3, 2009
8:31 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help