I've started using Knockout JS and I applied a textarea
counter to my viewmodel. But for some reason I get the error message:
TypeError: self.description(...) is undefined
var counter = self.description().length;
Even my teset fiddle doesn't work.
My code is this:
//HTML
<textarea data-bind="value: description, valueUpdate: 'afterkeydown'"></textarea>
//JS
jQuery(document).ready(function($) {
ko.applyBindings(specialOfferPreviewModel());
});
function specialOfferPreviewModel() {
// -- This part works
var self = this;
self.promoTitle = ko.observable();
self.description = ko.observable();
self.fromDate = ko.observable();
self.toDate = ko.observable();
// To and from date
self.validPeriod = ko.computed(function() {
return self.fromDate + " - " + self.toDate;
}, self);
// -- And this part breaks it
self.count = ko.computed(function(){
var counter = self.description().length;
return counter;
});
}
What am I missing? Any help appreciated!
Update:
I'm not sure the difference of these two, but either works:
//1
function specialOfferPreviewModel(){}
ko.applyBindings(specialOfferPreviewModel());
//2
var specialOfferPreviewModel = function(){}
var vm = new specialOfferPreviewModel ();
ko.applyBindings(vm);
They both work