1

I have the following piece of code that fetches data from a server...

dataCollection.fetch();

The data returned is a list of objects...

evidenceCollection.fetch().done(function(data) 
{ 
    console.log(data) //['test', 'test2'] 
});

I need this data to create a view...

var test_vw = new View({
    name: getName(),
    data: evidenceCollection.fetch().done(function(data) { return data })
});

This will not work as it throws errors. I also have the same error when I try to save data to a variable. What is the proper way to achieve what this?

buydadip
  • 8,890
  • 22
  • 79
  • 154
  • What is the error? – brk Apr 23 '19 at 14:44
  • Fetching the data is an asynchronous operation. If creating the view requires the data, then creating the view would have to be done in response to the asynchronous operation. That is, create the view in the callback. (Or create it first but without data, and add the data in the callback. It depends on how whatever framework you're using works.) – David Apr 23 '19 at 14:44
  • I'm guessing that you're expecting the `done(...)` to return the value in the function... and it isn't – freefaller Apr 23 '19 at 14:45
  • @David I dont want to do that as that will cause the view to wait for fetch call to finish – buydadip Apr 23 '19 at 14:46
  • @freefaller yeah it doesnt return – buydadip Apr 23 '19 at 14:47
  • @Bolboa: *"that will cause the view to wait"* - Again, this is an asynchronous operation. You don't *have* the data immediately, so you can't *use* the data immediately. You can only use it after you receive it. Can you create the view right away and feed the data to it after you get the data? For example, you might create it with a "loading spinner" by default, then in the async callback you would update the page with the data. – David Apr 23 '19 at 14:48
  • @David that could work – buydadip Apr 23 '19 at 14:51

0 Answers0