Note that there are some explanatory texts on larger screens.

plurals
  1. POProper way of dealing with templates and views in Knockout.js
    primarykey
    data
    text
    <p>I have <a href="http://jsfiddle.net/Osoascam/D87cg/6/" rel="nofollow">this fiddle</a> with the following view:</p> <pre class="lang-html prettyprint-override"><code>&lt;ul id="modules"&gt; &lt;li id="modulePersonal" data-bind="template: 'personalDataTemplate'"&gt;&lt;/li&gt; &lt;li id="moduleAccounts" data-bind="template: 'accountsDataTemplate'"&gt;&lt;/li&gt; &lt;/ul&gt; &lt;script type="text/x-jquery-tmpl" id="personalDataTemplate"&gt; &lt;h3 class="klavikaBold"&gt;Your Details&lt;/h3&gt; &lt;div class="content"&gt; {{if loggedIn}} &lt;p&gt;This is what you've been telling us:&lt;/p&gt; &lt;dl class="pairDefinition"&gt; &lt;dt&gt;Name&lt;/dt&gt; &lt;dd&gt;${name}&lt;/dd&gt; &lt;dt&gt;Gender&lt;/dt&gt; &lt;dd&gt;${gender}&lt;/dd&gt; &lt;dt&gt;Age&lt;/dt&gt; &lt;dd&gt;${age}&lt;/dd&gt; &lt;dt&gt;Country&lt;/dt&gt; &lt;dd&gt;${country}&lt;/dd&gt; &lt;/dl&gt; &lt;/div&gt; {{else}} &lt;p&gt;Hey, you Alpha male! Come here and give us all of your personal data! We promise we won't give it for free to anybody!&lt;/p&gt; {{/if}} &lt;/div&gt; &lt;/script&gt; &lt;script type="text/x-jquery-tmpl" id="accountsDataTemplate"&gt; &lt;h3&gt;Your accounts&lt;/h3&gt; &lt;div class="content"&gt; &lt;p&gt; There are 2 messages from Yahoo, 1 from Lumosity and 5 from Tweetland&lt;/p&gt; &lt;ul class="listSeparated line"&gt; &lt;li&gt; &lt;dl class="pairDefinition"&gt; &lt;dt&gt;Yahoo&lt;/dt&gt; &lt;dd&gt;&lt;a href="#"&gt;Account Details&lt;/a&gt;&lt;/dd&gt; &lt;dt&gt;Email&lt;/dt&gt; &lt;dd&gt;blabisbla@yahoo.com&lt;/dd&gt; &lt;/dl&gt; &lt;/li&gt; &lt;li&gt; &lt;dl class="pairDefinition"&gt; &lt;dt&gt;Lumosity&lt;/dt&gt; &lt;dd&gt;&lt;a href="#"&gt;Account Details&lt;/a&gt;&lt;/dd&gt; &lt;dt&gt;Username&lt;/dt&gt; &lt;dd&gt;ilovetheiradds&lt;/dd&gt; &lt;/dl&gt; &lt;/li&gt; &lt;li&gt; &lt;dl class="pairDefinition"&gt; &lt;dt&gt;Tweet Land&lt;/dt&gt; &lt;dd&gt;&lt;a href="#"&gt;Account Details&lt;/a&gt;&lt;/dd&gt; &lt;dt&gt;Username&lt;/dt&gt; &lt;dd&gt;costaricangamer&lt;/dd&gt; &lt;dt&gt;Games&lt;/dt&gt; &lt;dd&gt;Lovecity, CarRace, Unit&lt;/dd&gt; &lt;/dl&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/script&gt; </code></pre> <p>And this code:</p> <pre><code>(function($) { var userDetails = { "name": "Óscar Acuña", "country": "Costa Rica", "province": "San José", "district": "San Andrés", "zip": "123456", "birthday": "12/12/1990", "gender": "Male", "hotnessIndex": "6.8", "smell": "Nice if not sweaty" }; // Overall viewmodel for this screen, along with initial state var viewModel = { loggedIn : "yeah", name: userDetails.name, country: userDetails.country, province: userDetails.province, gender: userDetails.gender, age: getAgeSortOf(userDetails.birthday) + " years old" }; ko.applyBindings(viewModel); function getAgeSortOf(birthday) { return new Date().getFullYear() - new Date(birthday).getFullYear(); } })(jQuery); </code></pre> <p>Please note I elaborated the template just for the first li.</p> <p>Let's imagine there might be up to 10 <code>li</code>s, each containing different data (so the idea of them sharing a single view is unlikely). The data comes from a vendor, so they can change the names and order when they see fit.</p> <p>You see one template stores the different states it might have. My question is, do you think that's a secure approach? After all, the real data is being feed by an Ajax request (here represented by a hard-coded JSON userDetails).</p> <p>In addition, which is the most flexible way of setting variables that determine flow? (for example loggedIn : "yeah", if you remove it, the other view is displayed). Here I'm coding them right in the viewModel, but they can actually be coded outside... Is this the right approach or I'm missusing knockout? I've never dealt with a MVVM framework in front end so I might be a little confused.</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.
    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