Note that there are some explanatory texts on larger screens.

plurals
  1. POUse Requirejs to dynamically load Knockout AMD template & modules asynchronously facing race condition
    primarykey
    data
    text
    <p>I'm using Knockout, Require and <a href="https://github.com/rniemeyer/knockout-amd-helpers" rel="nofollow">knockout-amd-helpers</a> in my app. </p> <p>I want to load models dynamically inside an <code>Knockout initialize</code> function which will be called many times, since I don't know what the models, figured this might be a more efficient way to dynamically load rather than load all models at once. See code below:</p> <pre><code>ko.utils.extend(ModelA.prototype.initialize = function(data, parent) { ... require(['model/Pretag' + ko.unwrap( data.code )], function(Role){ roleInstance = new Role(parent); roleInstance.initialize(data.properties); .... self.roleObservable( roleInstance ); } ... }) </code></pre> <p><strong>The Problem</strong>: with this approach is, often times, I will run into race condition from the template's inline JS code since the <code>roleInstance.initialize(data.properties);</code> has yet to finish initialize.</p> <p>Is there anyway to enforce multiple <code>require([..],function(){})</code> load asynchronously before the template start to execute any inline JS calls ?</p> <p><strong>Update</strong>: inline JS code inside the templates are as follow:</p> <pre><code>&lt;li&gt;Type: (&lt;span data-bind="text: getObject('CODE').getProperties().STATIC_TYPE"&gt;&lt;/span&gt;)&lt;/li&gt; </code></pre> <p>This will throw an error, since <code>getObject('CODE').getProperties()</code> is undefined because <code>getObject('CODE')</code> has yet to finish initialize</p> <p>The list is included by an inline ko template below.</p> <pre><code>&lt;!-- ko template: { name: 'template_contains_problem_inline_JS', foreach: people } --&gt;&lt;!-- /ko --&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