• If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Announcement

Collapse
No announcement yet.

[SOLVED] the dreaded scope...?? (timing issues)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [SOLVED] the dreaded scope...?? (timing issues)

    If you want to use a variable sent back by an Ajax request, how can you make the function "wait for it"...
    in the code below, there are 2 console.log(pdfurl)... one inside the ajax and one outside... the outside seems to run before the one inside the ajax.. so my code that needs to use that variable doesn't work... I really don't want to put the other code inside the Ajax request.. any suggestions?

    for example:
    Code:
        onDoubleClickThumbnail: function(view, rec, item, index, e, eopts) {
    		var clid = rec.get('CLNUMBER');
    		var bkid = rec.get('BKID');
    		var pgid = rec.get('PGID');
    		var imgpath = rec.get('PGPATH');
            var pdfurl;
            var me = this;
            
    		Ext.Ajax.request({
    			url: 'vvcall.pgm',
    			params: {
    				pgm: 'mcb002r',
    				action: 'showpdf',
    				clid: clid,
    				bkid: bkid,
    				pgid: pgid,
    				imgpath: imgpath
    			},
    			scope: me,
    			success: function(response) {
    				var d = Ext.decode(response.responseText);
    				if (d.SUCCESS === '1') {
    					pdfurl = '/cache/' + d.PDFDOC; 
                                            console.log(pdfurl);   //<-works....
    				} else {
    					alert('error');
    				}
    			}
    		});
            console.log(pdfurl);  //<--returns undefined...

  • #2
    You are setting the value of pdfurl after the return of the ajax call (in the success function).

    Comment


    • #3
      Originally posted by mwmayer4 View Post
      I really don't want to put the other code inside the Ajax request..
      Is there a reason why you can't stick the code inside your Ajax success function?

      Comment


      • #4
        why... because it's a lot of code and I wanted it to be in another function that i may want to call from other places.. just trying to keep it much easier to read and follow..

        Comment


        • #5
          So just create a function with your logic outside of the ajax call and then within the sucess of the ajax call put the call to your function. That will keep it clean.

          Comment


          • #6
            Sometimes I make this stuff more complicated than it is. I guess I figured if the variable wasn't available after the Ajax, I wouldn't be able to do that. But it worked like a charm.
            Thanks!

            Comment

            Working...
            X