I have a feeling I’m missing something simple here, but just can’t quite grok this. Here is my script:
function FormDefinition()
{
var self = this;
self.Fields = ko.observableArray([new FieldDefinition()]);
}
function FieldDefinition()
{
var self = this;
self.Name = "Test";
}
function ViewModel()
{
var self = this;
self.formDef = ko.observable(new FormDefinition());
self.Name = "bob"
self.addField = function(){
this.formDef().Fields().push(new FieldDefinition());
}
}
ko.applyBindings(new ViewModel());
and here is my markup:
<a data-bind="click: addField">Add</a><br/>
<span data-bind="text: Name"></span>
<ul data-bind="foreach: formDef().Fields">
<li data-bind="text: Name"></li>
</ul>
and here is a jsFiddle: http://jsfiddle.net/5xSmr/
Expected behavior is that clicking ‘Add’ would cause the ui to update. debugging reveals that addfield is getting called.
Fixed your fiddle: http://jsfiddle.net/5xSmr/2/
The main problem was that you were calling Fields() and not just Fields. Fields() return the unwrapped array and by pushing directly to it, ko would never know about it.