Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that your <code>return</code> method doesn't return what you think it does. Your <code>return</code> function returns what <a href="http://api.jquery.com/jQuery.getJSON/" rel="nofollow noreferrer"><code>getJSON</code></a> returns and <code>getJSON</code> is an AJAX call. jQuery's AJAX calls all return <a href="http://api.jquery.com/Types/#jqXHR" rel="nofollow noreferrer"><code>jqXHR</code>s</a> and that has nothing to do with what the AJAX callbacks return. The sequence of events looks something like this:</p> <ol> <li>You call <code>JsonParser.return</code>.</li> <li><code>return</code> calls <code>$.getJSON</code>.</li> <li><code>getJSON</code> makes an asynchronous call to the server and then returns a <code>jqXHR</code>.</li> <li><code>return</code> returns the same <code>jqXHR</code> that <code>getJSON</code> returns.</li> <li>Some time passes.</li> <li>The server gets around to responding and your callback is called to search through the JSON.</li> </ol> <p>The problem is that while <strong>(5)</strong> is happening, the code that called <code>JsonParser.return</code> is still going but it has no JSON.</p> <p>The usual way to get around the asynchronous nature of AJAX calls is to send in a callback function that will get called when something is ready:</p> <pre><code>@return: (json_file_path, string_to_query, callback) -&gt; $.getJSON json_file_path, (data) -&gt; $.each data, (key,val) -&gt; if key == string_to_query callback(val) </code></pre> <p>Then you'd use it like this:</p> <pre><code>JsonParser.return("file.json", "key", (value) -&gt; # Do whatever needs to be done with value in here. ) </code></pre> <p>Now you have something that should work and you can find out how to use Jasmine with it over here:</p> <blockquote> <p><a href="https://stackoverflow.com/q/4662641/479863">How do I verify jQuery AJAX events with Jasmine?</a></p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload