开发者

Why is my function returning "function" and not an integer result?

I have the following 开发者_C百科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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜