1

I have the following function:

viewModel.unreadCount = ko.dependentObservable(function() {   
    var unreadCount = 0;

    for (var i = 0; i x< xxxxxxxxxxxx.length; i++) {
        if (xxxxxx == false) {
            unreadCount++;
        }
    }

    return unreadCount;
}, viewModel);

When I use this in KnockoutJS, I can't do a simple if (viewModel.unreadCount()==0), like this:

<div data-bind="visible: viewModel.unreadCount()==0">

It turns out because when I run:

<p>${ (typeof viewModel.unreadCount) }</p>

I get "function".

Any ideas why that is and how I can get it to return an INT so I can do an if statement?

1
  • 2
    ko.dependentObservable() returns a function, not the return value of the anonymous function you pass in. Commented Dec 31, 2010 at 21:54

1 Answer 1

2

Since ko.dependentObservable() is returning a function, as BoltClock pointed out, you should be able to call the function it returns by using this somewhat odd-looking syntax:

<div data-bind="visible: (viewModel.unreadCount)()==0">

This will then perform function application on the function that is returned, rather than just the unreadCount member of viewModel.

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.