Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a javascript singleton to return data via $.post?
    primarykey
    data
    text
    <p>I would like to create a method on the singleton <code>dp.DatapodManager</code> so that it loads the data once via $.post, then I can use that data by calling methods on the singleton.</p> <p>The output of the following code is:</p> <pre><code>111 222 test data </code></pre> <p>What do I have to do to get <code>dp.DatapodManager.loadDirectly();</code> to add the contents of the text file to <code>div#content</code>?</p> <p><img src="https://i.stack.imgur.com/wT9cn.png" alt="enter image description here"></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;test load&lt;/title&gt; &lt;script type="text/javascript" src="js/jquery-1.7.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('div#content').append('&lt;p&gt;1111&lt;/p&gt;'); $('div#content').append('&lt;p&gt;222&lt;/p&gt;'); $('div#content').append(dp.DatapodManager.getTestData()); dp.DatapodManager.loadDirectly(function(html) { $('div#content').append(html); }); }); var dp = dp || { VERSION : '0.00.05' }; dp.DatapodManager = (function() { return { loadDirectly: function(callback) { dp.qsys.loadDataFromExternalWebsite(function(stringBlock) { var lines = dp.qstr.convertStringBlockToLines(stringBlock); var html = dp.qstr.appendBrToLines(lines); callback(html); //never executes, cannot set breakpoint here in firebug }); callback('&lt;p&gt;this is returned&lt;/p&gt;'); }, getTestData: function() { return 'test data'; } } }()); dp.qsys = { loadDataFromExternalWebsite : function(callback) { url = 'http://localhost/webs/dpjs/data/data.txt'; var json = ''; (function() { var json = null; $.post(url, {}, function(jsonString) { callback(jsonString); }); return json; })(); } }; dp.qstr = { convertStringBlockToLines: function(block, trimLines) { var trimLines = trimLines || true; var lines = block.split(dp.qstr.NEW_LINE()); if(trimLines &amp;&amp; lines.length &gt; 0) { for(x=0; x&lt;lines.length; x++) { lines[x] = lines[x].trim(); } } return lines; }, NEW_LINE: function() { return '\r\n'; }, appendBrToLines: function(lines) { var r = ''; if(lines.length &gt; 0) { for(x=0; x&lt;lines.length; x++) { r += lines[x] + '&lt;br/&gt;'; } } return r; } }; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="content"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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