I have this code:
<script>
var viewModel = {};
$.getJSON("URL", function (data) {
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
</script>
I bind it in this code:
<!-- ko foreach: { data: object, as: 'object' } -->
<div>
... content (with more data-bindings) ...
</div>
<!-- /ko -->
But I get these errors in the console:
Uncaught TypeError: Unable to process binding "foreach: function(){return { data:object, as:'object'} }"
Message: Unable to process binding (...)
Message: Cannot read property (...)
I'm sure it's because the observable(s) are not pre-defined and by the time knockoutJS tries to bind them in, they're simply not existing.
I tried this:
var viewModel = {
object: null
(and other properties)
};
and this:
function viewModel() {
var self = this;
...
}
But it doesn't help. What am I doing wrong? I know there are plenty questions about that, but I can't find an answer to my problem.