Note that there are some explanatory texts on larger screens.

plurals
  1. POKnockout / Complex Object Issue
    primarykey
    data
    text
    <p>Let me preface this question by stating that I have only started working with the Knockout library yesterday afternoon so this quite possibly could be a factor in this issue.</p> <p>Before I get into the code, I am trying to create a model that a Competition object which has a Name property and a Criteria property which is a collection of Criterion objects. A Criterion object has a PrimaryDescription and SecondaryDescription property.</p> <p>Here is what I have so far:</p> <pre><code>function Competition(name, criteria) { var self = null; self = this; self.Name = ko.observable(name); self.Criteria = criteria; return; } function Criterion(id, primaryDescription, secondaryDescription) { var self = null; self = this; self.Id = id; self.PrimaryDescription = ko.observable(primaryDescription); self.SecondaryDescription = ko.observable(secondaryDescription); return; } </code></pre> <p>In the view model, I have a Competition property that is set up like this:</p> <pre><code>self.Competition = ko.observable(new Competition('Competition 1', ko.observableArray([new Criterion(-1, 'Criterion 1', 'Criterion 1'), new Criterion(-1, 'Criterion 2', 'Criterion 2')]))); </code></pre> <p>When the page loads, a binding to Competition().Criteria() works as expected.</p> <p>In the view model, I have added the following:</p> <pre><code>self.addCriterion = function () { self.Competition().Criteria().push(new Criterion(-1, '???', '???')); alert(self.Competition().Criteria().length); return; } </code></pre> <p>When I call this method, the new element gets added to the array and the array length is accurately reflected. However, the binding is not updated. I messed around a bit and changed the line in the Competition model part from "self.Criteria = criteria" to "self.Criteria = ko.observableArray(criteria)". When I do this and call the addCriterion method, the new element is added to the array and the binding updates the UI but the length of the array is reported as zero.</p> <p>Since I can only get this working halfway, this tells me I am doing something wrong. What am I missing?</p> <p>Many thanks in advance to whoever can steer me in the right direction.</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.
 

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