Loading ...
Sorry, an error occurred while loading the content.

Re: [ydn-javascript] Re: Callback success in namespace not executing in namespace?

Expand Messages
  • Satyam
    Globals should be avoided as much as possible. Normally a page is a mixture of code you wrote and libraries from external sources, ad providers, mashups and so
    Message 1 of 4 , Oct 1, 2008
    • 0 Attachment
      Globals should be avoided as much as possible. Normally a page is a mixture of code you wrote and libraries from external sources, ad providers, mashups and so on.   If you have full control of your page, it should not be a problem but if you don't there will be trouble.  You might have full control now but suddenly you think that adding some Map software here or there might be cool, and there you have an external library with a non-trivial chance of collision with your global names.  So, it doesn't matter where you set them, the issue is, don't let them become global.  YAHOO is already well known name and is, in practice, reserved so nobody would use them in anything that might become global, but beyond that, you are taking your chances.

      Notice that all variables you set within the anonymous function can be accessed simply by their names, as you are doing with textObj, you don't need to create an object to hold them.  The technique of creating an object (specially, as in this case, when it won't be instanced) is used to avoid taking up global name space, you create a single object and then put everything under it.  When you already provide hiding by the anonymous function, it serves no purpose to further hide them under an object.

      Regarding setting the scope for the callback, amongst the properties of the callback object there is one called "scope", which you may not need if you drop the testObj itself, see how simple it gets, and quite linear as well, instead of having lots of labeled pieces of code which, in order to read, you have to put together following references to named items, things are right where they are used, you can follow through the code in one sequence, except for those that are used more than once, which then do get a name:

      (function(){
      	var 	CON = YAHOO.util.Conn,
      		EVENT = YAHOO.util.Event,
      		DOM = YAHOO.util.Dom,
      
      		variableName = "INIT";
      
      	var logit = function (str) {
      	    var LOG = DOM.get('log');
      	    LOG.innerHTML += str + "<BR>";
      	};
      
      	var dump = function() {
      	        logit(variableName);
      	};
      
      	EVENT.onDOMReady(function(){
      		dump();
      	        CON.asyncRequest('GET','http://callbackurl',{
      			success : function(r){
      				variableName = "YES";
      				dump();
      			},
      			failure : function(r){
      				variableName = "NO";
      				dump();
      			}
      		});
      	});
      }());
      I think I will call this style the "simple tale" while the other is the "mystery novel", you know, you start developing the characters, giving them names and features and all the while you are asking yourself, how did it all get to this? and then, in the end, you learn that it all came out of onDOMReady.  The simple tale doesn't have that much mystery, not many characters (I have three shortcuts, two function names and one variable and that's all) and no suspense.

      Satyam


      Jeff wrote:
      Thanks for the feedback Satyam.

      I'll be sure to use JSLint from now on.

      > Since this doesn't seem to be a library which should have a publicly
      > accessible reference to it, and you already have it all enclosed in an
      > anonymous function, I wonder why would you care to declare a namespace
      > and bother with scoping and such, it can all be contained within the
      > anonymous function without 'this' and that.

      I was building off of one of the YUI examples for drag and drop.

      Ideally I would like to establish a method of developing small applications such that they would all be initialized through the onDOMReady event, like App1.init(), App2.init(), etc. I was experimenting with YAHOO's namespaces when I came to the previously described problem. I included in the bottom of this reply what I had done in the past. (var obj = {...})

      > your shortcut variables CON, EVENT and DOM will be in the global namespace

      I had intended for that to happen -- I wanted to stick them in a "common" file. Sticking them in the anonymous function was not quite what I had in mind. I would like to do something like:

      //common.js
      var CON=...
      var DOM=...
      var EVENT=...

      // app1.js
      var app1 = {...}

      ** This leads to another question. Would it be better to set the global vars in multiple anonymous functions (I guess they wouldn't be global then)? or in one common function?

      > The scope is set correctly when the callback
      > is set "this.callbackSuccess" is found without a problem, but when the
      > callback gets executed, the scope is 'window' and there is no
      > 'window.callbackSuccess'.

      In this case, is there an accepted way of setting the scope for the callback?



      -----------------------------

      This is what I have done (roughly) in the past to accomplish the same thing:
      -----------------------------
      //common
      var CON = YAHOO.util.Conn ect;
      var EVENT = YAHOO.util.Event;
      var DOM = YAHOO.util.Dom;
      function logit(str) {
          var LOG = document.getElementById('log');
          LOG.innerHTML += str+"<BR>";
      }

      //application
      (function(){

      var testobj = {
          variableName : "INIT",
          init : function(){
              testobj.dump();
              testobj.doCallback();
          },
          dump : function() {
              logit(testobj.variableName);
          },
          doCallback : function() {
              var callback = {
                  success : testobj.callbackSuccess,
                  failure : testobj.callbackFailure
              };
              CON.asyncRequest('GET','http://callbackurl',callback);
          },
          callbackSuccess : function(r){
              testobj.variableName = "YES";
              testobj.dump();
          },
          callbackFailure : function(r){
              testobj.variableName = "NO";
              testobj.dump();
          }
      };

      EVENT.onDOMReady(testobj.init, testobj, true);

      }());

      -----------------------------


      --- In ydn-javascript@yahoogroups.com, Satyam <satyam@...> wrote:
      >
      > Two things, the one you are asking for is that while you are properly
      > setting the scope for the init function, you are not doing so for the
      > callback to the asyncRequest call.  Besides the success and failure
      > settings in the callback object, you have to set the scope property to
      > have that fixed as well.  The scope is set correctly when the callback
      > is set "this.callbackSuccess" is found without a problem, but when the
      > callback gets executed, the scope is 'window' and there is no
      > 'window.callbackSuccess'.
      >
      > The other issue that JSLint would certainly report (it is a good idea to
      > use it) is that your shortcut variables CON, EVENT and DOM will be in
      > the global namespace, even while you took care to put them inside the
      > anonymous function.  They are missing the "var" in front of them.  As
      > they are, you are simply using those variables by assigning them
      > values.  You are not declaring it, you are simply using them.  Anything
      > you use that is not declared, gets implicitly declared in the global scope.
      >
      > Since this doesn't seem to be a library which should have a publicly
      > accessible reference to it, and you already have it all enclosed in an
      > anonymous function, I wonder why would you care to declare a namespace
      > and bother with scoping and such, it can all be contained within the
      > anonymous function without 'this' and that.
      >
      > Satyam
      >

      No virus found in this incoming message. Checked by AVG - http://www.avg.com Version: 8.0.169 / Virus Database: 270.7.5/1697 - Release Date: 29/09/2008 7:40
    Your message has been successfully submitted and would be delivered to recipients shortly.