0

Please check it here: http://jsfiddle.net/9VaW2/1/

I want jData be available outside

$http({method: 'POST', url: '/someurl'}).
  success(function(data, status, headers, config) {
    var jData = data;
  }).
    error(function(data, status, headers, config) {
  });

But when I do this:

$view.availableData = {
                cols:{ID:'ID', Date:'Date'},
                rows:{data:jData},
                options:{}
            };
   
    

It gives me this error

ReferenceError: jData is not defined

EDIT update fiddle

http://jsfiddle.net/9VaW2/3/

** I think I found a solution to make a synchronous call ..async : false. that works. I think the page was loading faster than the data was coming back.... wonder if someone has a better solution **

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
  • even if you would delete the "var" before jData, so it would be global variable it wouldn't probably set before you want to use it... – Gavriel May 15 '12 at 17:10

1 Answers1

0

Just define as a global variable.

var jData;

$http({method: 'POST', url: '/someurl'}).
    success(function(data, status, headers, config) {
      jData = data;
    }).
    error(function(data, status, headers, config) {
    });

$view.availableData = {
    cols:{ID:'ID', Date:'Date'},
    rows:{data:jData},
    options:{}
};
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
  • when I do console.log(jData) ...it gives me undefined – Asim Zaidi May 15 '12 at 17:16
  • @Autolycus, your jsFiddle demo is not working actually, can you correct it, or can we see a live example? – Okan Kocyigit May 15 '12 at 17:25
  • I know...its more complicated than just one function. Anyway I think I found a solution to make a syncronus call ..async : false. that works. I think the page was loading faster than the data was coming back....wonder if someone has a better solution – Asim Zaidi May 15 '12 at 17:27
  • @Autolycus, yes there might be a problem about async request as I said [here](http://stackoverflow.com/questions/10574247/iteration-function-call-in-ajax/10574419#10574419), different frameworks but It seems same problem. take all code into the success function, that should solve your problem. – Okan Kocyigit May 15 '12 at 17:34
  • that wouldnt solve my issue as its not that easy. :) tried that already by the way – Asim Zaidi May 15 '12 at 17:44
  • 1
    The global variable won't work properly. There's no guarantee that the callback will execute before the assignment to `$view`. Instead, you should move the assignment into the success callback. – Dominic Mitchell May 16 '12 at 07:28