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
> >
> >
>