I have a fairly simple view model to hold an array of data and take a string which I want to use to filter the data.
I have some very simple mark-up to render it like this:
<section class="task-list">
<ul data-bind="foreach:filteredRecords">
<li>
<label>Task name:</label>
<span data-bind="text:TaskName"></span>
</li>
</ul>
</section>
and I've set up the model as follows;
var viewModel = {
searchText : ko.observable(''),
taskData : ko.observableArray([]),
searchData : function () {
// use ko.utils.arrayFilter to limit records by searchText()
return this.taskData();
},
filterRecords: ko.computed(this.searchData, this).extend({ throttle: 500 }),
};
ko.applyBindings(viewModel, $('.task-list')[0]);
What I get is
Uncaught Error: Pass a function that returns the value of the ko.computed
moving the function back inside the ko:computed like this:
filteredRecords: ko.computed(function () {
return this.taskData();
},this).extend({ throttle: 500 })
just gives me this error insread:
Uncaught TypeError: Object [object global] has no method 'taskData'
So tried to define another property to use instead:
var viewModel = {
_self : this,
searchText : ko.observable(''),
taskData : ko.observableArray([]),
filteredRecords: ko.computed(function () {
return this.taskData();
},this).extend({ throttle: 500 })
};
but this now gives the error
Uncaught ReferenceError: _self is not defined
finally I tried doing this:
var viewModel = {
searchText : ko.observable(''),
taskData : ko.observableArray([]),
filteredRecords: ko.computed(function() {
var me = this;
return function () {
return me.taskData();
}
},this).extend({ throttle: 500 })
};
Now this, does not throw any of the previous errors but also does not yield any results in the HTML either...
How do I correctly apply a ko.computed to an object so that I can use the observable and observableArray that I have inside the object?