1

I'm getting data from server using JQuery and JSON. I defined getBooksDoneFunc as variable because I need to be able to call this function not only once (when getBooks is done) . Unfortunately, I cannot call getBooksDoneFunc from inside of signInOK as window["getBooksDoneFunc"]();. Why? What is the best way to call this function?

function getBooks(){     return $.getJSON( "bookstore.json" );             }

var getBooksDoneFunc = function(json) { 
         $.each(json.books, function(i, json){ .......... });

}

getBooks().done(getBooksDoneFunc);

function signInOK(){
         window["getBooksDoneFunc"]();
}

PS. The idea for window["getBooksDoneFunc"](); was taken from SO answer

UPDATE:

var booksJSON = {};
window["getBooksDoneFunc"](booksJSON);

getBooksDoneFunc must be called with parameters nevertheless the call to getBooksDoneFunc fails. signInOK is defined outside of $(document).ready(function(){ }); but called inside of it.

Community
  • 1
  • 1
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
  • 2
    If this is all done enclosed in another function, say, `$(document).ready(function(){...});`, then `getBooksDoneFunc` won't exist under `window`. – ahren Apr 17 '13 at 00:03
  • @ahren sounds like an answer... – kojiro Apr 17 '13 at 00:05
  • @kojiro - unfortunately not, since I didn't answer the question *"How to invoke function call if it is defined as var?"* or *"What is the best way to call this function?"* – ahren Apr 17 '13 at 00:05
  • Thanks for the remark, indeed my func `signInOK()` is called inside `$(document).ready` while all other functions is defined outside of `$(document).ready` – J.Olufsen Apr 17 '13 at 00:18
  • 1
    Your getBooksDoneFunc() function takes one argument but you attempt to call without any inside signInOK(). – Ja͢ck Apr 17 '13 at 00:19
  • @ahren, how to call func if *it is not exist under `window`*? – J.Olufsen Apr 17 '13 at 00:39
  • 1
    What is the failure message? If the call succeeds but is made with no parameters then `json` is undefined and your `$.each` will throw an error about not being able to get the length to iterate over as `json.books` doesn't exist since `json` is undefined. – Dave Anderson Apr 17 '13 at 00:41
  • I defined and called `signInOK()` inside `$(document)ready` and now there is no problem. It just works. Thanks for help! – J.Olufsen Apr 17 '13 at 00:53

3 Answers3

2

Try:

function getBooks(){
  return $.getJSON( "bookstore.json" );
}

window.getBooksDoneFunc = function(json) { 
  $.each(json.books, function(i, json){ .......... });
}

getBooks().done(getBooksDoneFunc);

$(document)ready(function() {
  function signInOK(){
    var booksJSON = {};
    window.getBooksDoneFunc(booksJSON);
  }
});
Mathijs Flietstra
  • 12,900
  • 3
  • 38
  • 67
1

If window["getBooksDoneFunc"](); works, then does getBooksDoneFunc(), the idea of using window is when you want to access a global function but you don't know the function name which is stored in a variable.

In your case, put a hardcoding string is mean less, just do getBooksDoneFunc() is the same, because you already store the function self (not the string of function name) in the variable.

The thing that won't work is that if the variable is not global, please check the scope.

xdazz
  • 158,678
  • 38
  • 247
  • 274
0

I would do this a bit differently, although I do not really understand the signInOK() function. How will it receive the "json" data. I would reconstruct the getBooks function and rethink the signInOk function. Here's a start:

function getBooks() {
    $.getJSON("bookstore.json").done(function (json) {
        getBooksDoneFunc(json);
    });
}

var getBooksDoneFunc = function(json) { 
     $.each(json.books, function(i, json){ .......... });

};

...

getBooks();

function signInOK(){
     getBooksDoneFunc("some json data");
}
ron tornambe
  • 10,452
  • 7
  • 33
  • 60
  • 1
    `getBooks` returns the result of `$.getJSON` which is shorthand for `$.ajax` and that returns a `$.Defered` object which does have a `done` method. http://api.jquery.com/category/deferred-object/ – Dave Anderson Apr 17 '13 at 00:37