Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing multiple viewModels within knockout.js
    primarykey
    data
    text
    <p>I'd like to keep my knockout script more organized, moreover, I'd like to avoid naming 2 functions the same thing by accident. So I was wondering if I could nest viewModels in the same function like so (I kept it real simple): <a href="http://jsfiddle.net/likwid_t/KnBrE/" rel="nofollow">Fiddle</a></p> <p>Here's the HTML</p> <pre><code>&lt;p&gt;First name: &lt;strong data-bind="text: other.firstName"&gt;todo&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Last name: &lt;strong data-bind="text: other.lastName"&gt;todo&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Full name: &lt;strong data-bind="text: other.fullName"&gt;todo&lt;/strong&gt;&lt;/p&gt; </code></pre> <p>and the JS:</p> <pre><code>function AppViewModel() { var self = this; self.other = { firstName: ko.observable("Bert"), lastName: ko.observable("Bertington"), /*fullName: ko.computed(function(){ return this.firstName + " " + this.lastName; }, this)*/ } } </code></pre> <p>This works fine, but if I uncomment the ko.computed it'll crash. Is there any way to organize my knockout in this way, why is the computed crashing, is there any way to write the ko.computed function so it will work?</p> <p><strong>EDIT: Problem #2</strong></p> <p>If I have a form any form like this:</p> <pre><code>&lt;form data-bind="submit: other.otherSubmit" data-ajax="false"&gt; &lt;button type="submit"&gt;Submit&lt;/button&gt; &lt;/form&gt; </code></pre> <p>and I add a handler for submit like so:</p> <pre><code>// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI function AppViewModel() { var self = this; self.other = new function(){ var self = this; self.firstName = ko.observable("Bert"); self.lastName = ko.observable("Bertington"); self.fullName = ko.computed(function(){ return self.firstName() + " " + self.lastName(); }); self.otherSumbit = function(){} } } // Activates knockout.js ko.applyBindings(new AppViewModel()); </code></pre> <p>why does the error console return this:</p> <p><em>The value for a submit binding must be a function</em> </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