1
votes

After reading the Knockout documentation on Binding Context, I would expect

this HTML:

<div data-bind="with: blah">
    <pre data-bind="text: ko.toJSON($context, null, 2)"></pre>
</div>

and this JS:

var viewModel = {
    blah: {
        hello: "hello",
    }
};

ko.applyBindings(viewModel);

would print this:

blah: {
    hello: "hello",
}

However, I get the following error:

Error: Unable to parse bindings. Message: Error: Pass a function that returns the value of the ko.computed; Bindings value: text: ko.toJSON($context, null, 2)

How should it be used?

JsFiddle for reference.

1

1 Answers

0
votes

Just figured it out. $context and $parentContext just hold the special properties for that context. So in order to get what I was expecting I had to change the HTML to:

<div data-bind="with: blah">
    <pre data-bind="text: ko.toJSON($context.$data, null, 2)"></pre>
</div>

Updated JsFiddle