I am learning knokcout.js
I was using the example in http://www.knockmeout.net/2011/04/utility-functions-in-knockoutjs.html, which is working fine.
I added the following to the javascript there.
viewModel.breads = ko.dependentObservable(function() {
return ko.utils.arrayFilter(this.items(), function(item) {
return item.category()=='Bread';
});
}, viewModel);
It was working.
Then I added the follwing
viewModel.breadsLength = ko.dependentObservable(function() {
var filtered_array= ko.utils.arrayFilter(this.items(), function(item) {
return item.category()=='Bread';
});
return filtered_array().length;
}, viewModel);
This makes the whole javascript fail. I tried filtered_array.length(), filtered_array().length(), this.breads.length() etc. I was not able to get it to work.
Displaying array length by data-bind="text: breads().length" works only once and does not seem to update whenever I use a new json.
In this case, you would just want to do:
filtered_array.length. The result ofko.utils.arrayFilteris a normal array and you would be checking thelengthproperty of it (which is not an observable).