I have the following html/js existing working code that I want to extract in their own files and load them using require.js.
My goal is to componentize it to be able to use it elsewhere.
.html
...
<ul data-bind="foreach: selectedExams">
<li>
<select data-bind="options: $parent.availableExams, optionsText: 'examTypeName', optionsValue:'examTypeId', value: examtype"></select>
<select data-bind="options: exams, optionsText: 'examName', optionsValue:'examId',value: exam, enable:exams().length"></select>
<a href="#" data-bind="click: $parent.remove">Remove</a>
</li>
</ul>
<button data-bind="click: add">Add</button>
...
.js
var self = this;
...
self.availableExams = [...];
self.selectedExams = ko.observableArray([new selectedExam(self)]);
self.add = function () {
self.selectedExams.push(new selectedExam(self));
};
self.remove = function (exam) { self.selectedExams.remove(exam) }
...
It seemed very simple at first, but I am confused because the viewmodel seems to need a complete rewrite.
Because I want to experiment and see how it works, following the docs, I've created a separate .html file with exactly the required content show above.
And the following autonomous .js file:
function ExamControlViewModel()
{
var self = this;
self.availableExams = [...];
self.selectedExams = ko.observableArray([new selectedExam(self)]);
self.add = function () {
self.selectedExams.push(new selectedExam(self));
};
self.remove = function (exam) { self.selectedExams.remove(exam) }
}
ko.applyBindings(new ExamControlViewModel());
Next, I've removed the code from the original html/js files and added the following to the js file :
ko.components.register('exam-control', {
viewModel: { require: 'exam-control-viewmodel' },
template: { require: 'text!exam-control-view.html' }
})
It fails at runtime with the following error :
Script error for "text", needed by: text!exam-control-view.html_unnormalized2 http://requirejs.org/docs/errors.html#scripterror
Obviously I'm missing the point, This could not be that simple... Could it be ?
Any guidance appreciated.
EDIT 1:
I followed @JotaBe excellent instuctions and ended up with the following :
.js file
define([],function() {
function ExamControlViewModel()
{
var self = this;
self.availableExams = [...];
self.selectedExams = ko.observableArray([new selectedExam(self)]);
self.add = function () {
self.selectedExams.push(new selectedExam(self));
};
self.remove = function (exam) { self.selectedExams.remove(exam) }
}
return ExamControlViewModel;
});
html file: => At the beginning
...
<script type='text/javascript' src='Scripts/knockout-3.4.0.js'></script>
<script type='text/javascript' src='Scripts/knockout.validation.js'></script>
<script type='text/javascript' src='Scripts/require.js'></script>
<script type='text/javascript' src='Scripts/text.js'></script>
...
=> The component code was replaced by (forgot to say that when asking the question)
<exam-control></exam-control>
Now at runtime, I have the following error:
Mismatched anonymous define() module
I'll update this thread again as I make progress.