Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help assigning new value to global variable.

    I am working with the google blogger API, and I am having an issue updating my global variable Response. I thought I understood how global variables worked, so I don't know if there is something different about the blogger API or if I'm making a dumb mistake.

    Code:
    <SCRIPT TYPE="text/javascript" src="http://www.google.com/jsapi"></ 
    script> 
    <script> 
    google.load("gdata","1.x", {packages: ["blogger"]}); 
    google.setOnLoadCallback(getMyBlogFeed); 
    blogID = "1601946089552390859"; 
    var feedUri = "http://www.blogger.com/feeds/"+blogID+"/posts/full?alt=json"; 
    var Response = ""; 
    function getMyBlogFeed(){ 
            var myBlog = new google.gdata.blogger.BloggerService('GoogleInc-jsguide-1.0'); 
            myBlog.getBlogPostFeed(feedUri, handleBlogFeed, handleError); 
    } 
    
    function handleBlogFeed(myResultsFeedRoot) { 
            Response = myResultsFeedRoot.feed.entry[0].content.$t; 
    } 
    
    function handleError(e) { 
            alert("There was an error in getBlogPostFeed"); 
            alert(e.caue ? e.cause.statusText : e.message); 
    } 
    
    alert(Response); 
    </SCRIPT>
    Here's what I think SHOULD happen:
    1. Response is initialized as a global variable with a value of ""
    2. setOnLoadCallback calls getMyBlogFeed which creates a blog object, then calls getBlogPostFeed which calls handleBlogFeed.
    3. handleBlogFeed stores a new string to global variable Response
    4. an alert pops up with the value of Response (as given by handleBlogFeed)


    What actually happens is that the alert pops up with the original value of Response. I know that I can issue the alert inside handleBlogFeed, but that isn't the issue. My issue is that I'd like to use Response in other functions, but it isn't being updated as a global variable. What am I doing wrong that Response isn't updating?

    On a related note, is there a way to return a variable from my handleBlogFeed function? How would I do that? -- I can't seem to figure it out.

    Thanks!

    P.S. I recognize that the best place to ask this is the blogger developer group. I've already posted this there, and no one has responded yet.
    Last edited by jnwbbr; 04-02-2011 at 01:13 AM.

  • #2
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by jnwbbr View Post
    1. Response is initialized as a global variable with a value of ""
    2. setOnLoadCallback calls getMyBlogFeed which creates a blog object, then calls getBlogPostFeed which calls handleBlogFeed.
    3. handleBlogFeed stores a new string to global variable Response
    4. an alert pops up with the value of Response (as given by handleBlogFeed)
    Your order is off. Number 4 happens actually between numbers 1 and 2; the onload callback is obviously deferred to some future point in time when the page will be loaded, whereas the alert will happen immediately. If you want to use Response in other functions, you will have to make sure that they won't be called before the onload callback has actually run.

    Quote Originally Posted by jnwbbr View Post
    On a related note, is there a way to return a variable from my handleBlogFeed function? How would I do that? -- I can't seem to figure it out.
    I'm sure you know how to let a function return some value, so I don't really understand the issue. Is it that the return value gets lost in the event handler? Where do you need that return value to be?

  • Users who have thanked venegal for this post:

    jnwbbr (04-02-2011)

  • #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by venegal View Post
    I'm sure you know how to let a function return some value, so I don't really understand the issue. Is it that the return value gets lost in the event handler? Where do you need that return value to be?

    Yes, it is exactly that the return value gets lost in the event handler. I know that the function should look like this:
    Code:
    function handleBlogFeed(myResultsFeedRoot) { 
            return Response = myResultsFeedRoot.feed.entry[0].content.$t; 
    }
    What I don't know, is what to do when I call the function. Is it:
    Code:
    myBlog.getBlogPostFeed(feedUri, returnedResponse = handleBlogFeed, handleError);
    Or
    Code:
    returnedResponse = myBlog.getBlogPostFeed(feedUri, handleBlogFeed, handleError);
    Or is it some hybrid mix of the two?

    I don't think it's any of those because I can't seem to get them to work.


    By the way, your explanation about why my global variable wasn't being assigned how I though was correct. I didn't even think about the order in which a webpage loads, so thank you.

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I don't understand why you would want the handleBlogFeed function to return anything. Any return value would be returned to the caller of the function, which is the myBlog.getBlogPostFeed function, which isn't interested in that return value at all, and won't do anything with it.

    So, please explain in more detail what exactly it is you are trying to accomplish here.

  • #5
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Code:
    <script>
    google.load("gdata","1.x", {packages: ["blogger"]});//, {"blogger"});
    blogID = "ASeriesOfNumbers";
    var feedUri = "http://www.blogger.com/feeds/"+blogID+"/posts/full?alt=json";
    var parseCoord = "";
    startLatLng = new google.maps.LatLng(0,0); //middle of ocean
    
    function runStart(){
       var myOps = {
          zoom: 13,
          center: startLatLng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    	var buzzMap = new google.maps.Map(document.getElementById("map_canvas"), myOps);
    	var buzzBlog = new google.gdata.blogger.BloggerService('GoogleInc-jsguide-1.0');
    	window.setTimeout(function(){reloadMap(buzzMap, buzzBlog)},8000);
    }
    
    function reloadMap(buzzMap, buzzBlog){
    	buzzBlog.getBlogPostFeed(feedUri, handleBlogFeed, handleError);
    	window.setTimeout(function(){reloadMap(buzzMap, buzzBlog)},8000);
    
    }
    
    function handleBlogFeed(myResultsFeedRoot) {
    	var parseCoord = myResultsFeedRoot.feed.entry[0].content.$t;
    	buzzCoord = parseCoord.split(",");
    	lat = parseFloat(buzzCoord[0]);
    	lng = parseFloat(buzzCoord[1]);
    	buzzLatLng = new google.maps.LatLng(lat, lng);
    	buzzMap.setCenter(buzzLatLng);
    	
    }
    function handleError(e) {
    	alert("There was an error in getBlogPostFeed");
    	alert(e.caue ? e.cause.statusText : e.message);
    }
      
    
    </SCRIPT>
    Sorry it's taken me so long to respond. Here is what I am trying to do: I will be updating a google blogger with latitudes and longitudes (not done with this code). I want this code to pull that data from the blogger and plot those coordinates in a google map.

    My issue right now is that within the handleBlogFeed() function, the variable "buzzMap" is undefined. I tried moving
    Code:
    var myOptions = {
          zoom: 13,
          center: startLatLng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    var buzzMap = new google.maps.Map(document.getElementById("map_canvas"), myOps);
    outside of the runStart() function to make it a global variable, but google didn't like that and threw some errors.

    What I need to do is get my "lat" and "lng" variables into the same scope with "buzzMap" so that I can update my google map, but I'm not really sure how to do that at this point. Any advice you could give would be great.

  • #6
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Quote Originally Posted by jnwbbr View Post
    My issue right now is that within the handleBlogFeed() function, the variable "buzzMap" is undefined. I tried moving
    Code:
    var myOptions = {
          zoom: 13,
          center: startLatLng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    var buzzMap = new google.maps.Map(document.getElementById("map_canvas"), myOps);
    outside of the runStart() function to make it a global variable, but google didn't like that and threw some errors.
    You can't access DOM objects before the DOM is actually loaded (or the maps API before it is loaded), so moving the whole thing out of that start handler (which presumably gets called after the DOM and the maps API are loaded) won't work.

    What you can do, though, if you need those things to be globally accessible is to declare those variables in the global scope (var myOptions, buzzMap;) and actually define them inside the start handler. Make sure you don't use the var keyword there, though, because that would result in new local variables instead of changing the already existing global ones.

  • Users who have thanked venegal for this post:

    jnwbbr (04-12-2011)

  • #7
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Your suggestion worked! Thanks!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •