I would like to understand why knockout behaves differently when a property is a prototype, and especially important, how to avoid it, while still using prototypes.
I want some methods to be overridden because I have a base view model that we are to inherit
The following demonstrates what I mean
JSFIDDLE WITH PROTOTYPE [try typing into the first input box and it will appear in the other]
var viewModel = function(params) {
this.params = params;
};
viewModel.prototype.text = ko.observable(this.params && this.params.initialText || '');
ko.components.register('message-editor', {
viewModel: viewModel,
template: 'Message: <input data-bind="value: text" /> '
+ '(length: <span data-bind="text: text().length"></span>)'
});
ko.applyBindings();
<!-- ko component: "message-editor" -->
<!-- /ko -->
<br />
<!-- ko component: "message-editor" -->
<!-- /ko -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
JSFIDDLE WITHOUT PROTOTYPE [try typing into the first input box and it will not appear in the other]
var viewModel = function(params) {
this.params = params;
this.text = ko.observable(this.params && this.params.initialText || '');
};
ko.components.register('message-editor', {
viewModel: viewModel,
template: 'Message: <input data-bind="value: text" /> '
+ '(length: <span data-bind="text: text().length"></span>)'
});
ko.applyBindings();
<!-- ko component: "message-editor" -->
<!-- /ko -->
<br />
<!-- ko component: "message-editor" -->
<!-- /ko -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
Why are they behaving differently?