1

I'm a newby in ko, but cannot find a solution for my problem. I try to organize my bindable properties into a view-model hierarchy. Based on the documentation it seems to me that the following should work, but it doesn't. Do you have any hints?

function AppViewModel() {
    this.nested = new NestedViewModel();
}

function NestedViewModel() {
    this.firstName = ko.observable();
     this.lastName = "Bertington";
}

ko.applyBindings(new AppViewModel());

and used here:

<p data-bind="with: nested">

<p>First name: <input type="text" data-bind="value: firstName, valueUpdate: afterkeydown"></input></p>
<p>Last name: <strong data-bind="text: firstName"></strong></p>

</p>

1 Answer 1

6

Your HTML is invalid. You cannot nest <p> elements. Therefore the browser auto-generates the closing tag as <p data-bind="with: nested"></p>.

Replace the outer wrapper with e.g. a <div> to make the HTML valid and the script working.

Also, it needs to be valueUpdate: 'afterkeydown' (with the quotation marks added), otherwise knockout looks for an observable named "afterkeydown".

Here is a working demo: http://jsfiddle.net/JwWCc/1/

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.