I'm experiencing with knockout.js components and require.js. This is working well so far, but I'm struggling with the following.
Let's say I have one instance of my component in a very simple html page :
<div id="exams">
<databound-exam-control></databound-exam-control>
</div>
From the containing viewmodel :
require(['knockout', 'viewModel', 'domReady!'], function (ko, viewModel) {
ko.components.register('databound-exam-control', {
viewModel: { require: 'databound-exam-control-viewmodel' },
template: { require: 'text!databound-exam-control-view.html' }
});
ko.applyBindings(new viewModel());
});
I would like to get the child viewmodel content to later save all the data of the page when I click a button.
For now I just trying to display the display the parent/child viewmodels in a pre tag :
<div>
<pre data-bind="text: childViewModel()"></pre>
</div>
With the help of the containing viewmodel :
function childViewModel() {
var model = ko.dataFor($('databound-exam-control').get(0).firstChild);
return ko.toJSON(model, null, 2);
};
I get the following error during the call to ko.dataFor, probably because the the page is not completely rendered :
Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.
Is that it ? Or am I completelty missing the point here ?
Any help appreciated.