Note that there are some explanatory texts on larger screens.

plurals
  1. POBest practice to send javascript code (e.g. a function, not a complete file) to the browser?
    primarykey
    data
    text
    <p>Assuming a JavaScript-based single-page application is returned by the server on the initial request. Besides some initialization code, which is common for every application, just the portion of the application needed to show the requested page (e.g. index page) is returned by the server on the initial request (then cached and rendered).</p> <p>As the user clicks through the application, other portions of the application should be asynchronously loaded ('fetched', 'requested' or however you wanna call it) from the server. By "portions" a mean javascript code, images, css, etc. required to render the page. Let's focus on the javascript code part in this discussion.</p> <p>It's important to notice that the javascript code to be returned to the browser is not contained in separate (static) files (which would be easy then and might be the case in the future due to e.g. performance reasons), but rather in one file, so it's not a 1:1 assiociation (request : file).</p> <p>E.g. we could have a single app defined like this:</p> <pre><code>var LayoutPresenter = App.Presenter.extend('LayoutPresenter', { __view: '&lt;div&gt;{{link "Author" "/author"}} - {{link "Book" "/book"}}&lt;/div&gt;' }); var AuthorPresenter = App.Presenter.extend('AuthorPresenter', { __view: '&lt;div&gt;&lt;h1&gt;{{name}}&lt;/h1&gt;&lt;/div&gt;', __parent: LayoutPresenter, __context: { name: "Steve" } }); var BookPresenter = App.Presenter.extend('BookPresenter', { __view: '&lt;div&gt;&lt;h1&gt;{{title}}&lt;/h1&gt;&lt;/div&gt;', __parent: LayoutPresenter, __context: { title: "Advanced JavaScript" } }); </code></pre> <p><em>App.Presenter</em> is part of the library I am writing and is available in the browser (or on any other client). </p> <p>So assuming the user is browsing to the <em>Book</em> page which hasn't be loaded before (neither initially nor cached in the browser), the <em>BookPresenter</em> code, which is a function, should be returned by the server (assuming the <em>LayoutPresenter</em> code is already available in the browser and <em>App.Presenter</em> is available anyway because it's part of the library). I am running node.js on the server side.</p> <p>How would you recommend to address this problem?</p> <p>There is the <em>eval</em> function, so one could send javascript as a string and bring it back to live using eval(), but such an approach seems to be bad practice.</p>
    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.
 

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