Note that there are some explanatory texts on larger screens.

plurals
  1. POPass data from server-side to YUI 3 JavaScript application
    primarykey
    data
    text
    <p>I am working on rewriting my application from YUI 2 to YUI 3.</p> <p>Sometimes I need some data from database in my JavaScript environment. Firs option is to assign some global variables in JavaScript, but global vars is not good, so I did following in YUI 2:</p> <p><strong>app.js</strong></p> <pre><code>YAHOO.namespace('MyApp'); YAHOO.MyApp = function() { var currencyRates; var userInfo; /* here a lot of code with event listeners and dom manipulations which uses currencyRates and userInfo variables */ return { initCurrencyRates: function(newRates) { currencyRates = newRates; }, initUserInfo: function(newUserInfo) { userInfo = newUserInfo; }, } }(); </code></pre> <p><strong>PHP</strong></p> <pre><code>&lt;?php $currencyRates = array('EUR' : 1.3245, 'GBP': 1.4322, 'RUB': 0.02334); //actually it comes from database print '&lt;script&gt;YAHOO.MyApp.initCurrencyRates(' . json_encode($currencyRates) . ')&lt;/script&gt;'; $userInfo = array('Name' =&gt; 'Jhon', 'ID' =&gt; 10); //actually it comes from database print '&lt;script&gt;YAHOO.MyApp.initUserInfo(' . json_encode($userInfo) . ')&lt;/script&gt;'; ?&gt; </code></pre> <p>As you can see I use "public methods" YAHOO.MyApp.initUserInfo and YAHOO.MyApp.initCurrencyRates to pass data into JavaScript code.</p> <p><strong>Now I what to rewrite it using YUI 3:</strong></p> <p><strong>app.js</strong></p> <pre><code>YUI().use('node', 'event', function(Y) { var currencyRates; var userInfo; /* here a lot of code with event listeners and dom manipulations which uses currencyRates and userInfo variables */ }) </code></pre> <p><strong>PHP</strong></p> <pre><code>&lt;?php $currencyRates = array('EUR' : 1.3245, 'GBP': 1.4322, 'RUB': 0.02334); //actually it comes from database print '&lt;script&gt;???&lt;/script&gt;'; ?&gt; </code></pre> <p>How do I provide "public methods" in my YUI 3 JavaScript code? Or what is another solution to pass data inside JavaScript application code aviding global variables?</p>
    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.
 

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