0

I have the following code:

class IndexViewModel {
    projects: KnockoutObservableArray<Project>;

    constructor() {
        this.projects = ko.observableArray<Project>([]);
    }

    filteredList: KnockoutComputed<Project[]> = ko.computed(() => {
        var elements = ko.utils.arrayFilter(this.projects(), (item: Project) => {
           //do something
        });
        //return something
        })
    }

But when I load the page, I get a JavaScript error, that says, TypeError: _this.projects is not a function index-ViewModel_test.js

The rendered JavaScript for that part is,

this.filteredList = ko.computed(function () {
            var elements = ko.utils.arrayFilter(_this.projects(), function (item) {
                //return something
            });
            //return something
        })

What am I missing here?

1 Answer 1

1

The problem was that the projects member was not yet defined when the filteredList was computed. Moving the computation in the constructor of the class solved my issue. The code:

constructor() {
        this.projects = ko.observableArray<Project>([]);
        this.filteredList = ko.computed(() => {
            var elements = ko.utils.arrayFilter(this.projects(), (item: Project) => {
                //return something
            });
            //return something
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.