1

I'm trying to figure out how to access a variable inside a .when.done function. here's the example:

var colviews = {
    1: true,
    2: true,
    3: false
}

$.when(  // run only -when- these script get loaded
    $.getScript( "/mckinney_images/jquery.tablesorter.all.js" )).done(function(){

    $(function(){
        console.log(colviews); // How do I get the colviews variable here?
    });

});

I have a basic understanding of scope but not sure how it applies inside a when function.

royhowie
  • 10,605
  • 14
  • 45
  • 66
MrP
  • 31
  • 3
  • 1
    `colviews` is a global variable so it could be access everywhere (but do not depend your local classes on global variables) – MaxZoom Jul 16 '15 at 19:30
  • Btw, you don't need that `$.when` at all, just use `$.getScript(…).done(…);` – Bergi Jul 16 '15 at 21:05
  • 1
    Your code most certainly does not reproduce the problem you claim it does. `colviews` will absolutely be available on the line where you write `console.log`. – meager Jul 16 '15 at 21:44
  • @meagar: Did he actually claim that it was not? – Bergi Jul 16 '15 at 21:52
  • 1
    @Bergi Yes? How else can you interpret this question? The line `console.log(colviews); // How do I get the colviews variable here?` seemingly indicates that the `console.log` is failing to print the expected value. This is *not* due to the variable being inaccessible as the comment implies. – meager Jul 16 '15 at 21:53
  • @meagar: Rather literally "*I understand how scope works, but am not sure what (if anything) is different inside a `$.when`*". But I'm not trying to argue :-) – Bergi Jul 16 '15 at 21:55
  • @meagar `colviews` is available in his code, but it itself has a problem that has been corrected in my post. – MaxZoom Jul 16 '15 at 21:55
  • 1
    @MaxZoom No, your post is unnecessary and adds nothing new. You do not need to "pass in" `colviews`, it's already in scope. – meager Jul 16 '15 at 21:56
  • @MaxZoom what do you think "invalid" means in this case? There is nothing wrong with that variable or its declaration. – meager Jul 16 '15 at 22:04

1 Answers1

0

I have a basic understanding of scope

Good! So you know what scope is, how closures work and how var works.

…but not sure how it applies inside a when function.

Simple: It's no different! Yes, the done callback function is invoked asynchronously, but that doesn't change how scope works.
You just are getting the colviews variable there successfully (if it doesn't log the expected value, that means it was overwritten elsewhere or the callback is never called because $.getScript failed).

$.when does no magic (and in fact, in your example it's not needed at all, your code works the same when you omit it).

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • @MaxZoom: Not sure whether I got this wrong, but my claim is that his code is already working. No need to improve anything (neglecting the minor `$.when` quibble). OP asked for understanding, not for fixing anything afaiks. – Bergi Jul 16 '15 at 21:51
  • Thanks, I figured this out later on in the day. – MrP Jul 17 '15 at 20:38