1

I'm having trouble understanding why this binding setup isn't working.

I have a Page object with an id and name, I have a pendingBatchDocument with a batchDocumentId observable and a pages observableArray. In my viewmodel, I'm trying to initialize an observable array with PendingBatchDocument's and initialize those PendingBatchDocuments with their array of Pages.

The syntax isn't giving me any errors so I assume the setup there is OK. Let me know if it isn't right.

My question is, why isn't the binding on the second foreach working?

View

<div data-bind="foreach: pendingDocs">
  <ul class="sortable" data-bind="foreach: pendingDocs().pages()">
  </ul>
</div>

Javascript View Model

function Page(id, name)
{
  this.id = ko.observable(id);
  this.name = ko.observable(name);
}

var PendingBatchDocument = function(batchDocumentId, pages)
{
  this.batchDocumentId = ko.observable(batchDocumentId);
  this.pages = ko.observableArray(pages);
};

var ViewModel = function()
{
  this.list1 = ko.observableArray([
    { itemId: "C1", name: "Item C-1" }, 
    { itemId: "C2", name: "Item C-2"}, 
    { itemId: "C3", name: "Item C-3"}, 
    { itemId: "C4", name: "Item C-4"}, 
    { itemId: "C5", name: "Item C-5"}, 
    { itemId: "C6", name: "Item C-6"}, 
    { itemId: "C7", name: "Item C-7"}]);

  this.pendingDocs = ko.observableArray([
    new PendingBatchDocument(1, [ 
      new Page(1, "Page 1"), new Page(2, "Page 2"), new Page(3, "Page 3") 
    ])
  ]);
};

ko.applyBindings(new ViewModel());

JSBin http://jsbin.com/ivavew/3/edit

1 Answer 1

2

The context inside the foreach binding is individual array element, which means inside foreach: pendingDocs, you already have the access to PendingBatchDocument instance, so you could use its pages property directly:

<div data-bind="foreach: pendingDocs">
    <ul class="sortable" data-bind="foreach: pages">
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.