Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:43:41+00:00 2026-06-13T20:43:41+00:00

I have this little ViewModel: function BooksViewModel() { var self = this; self.books =

  • 0

I have this little ViewModel:

function BooksViewModel() {
    var self = this;
    self.books = ko.observableArray(library);

    self.findByLanguage = function(lang) {
        self.books = ko.computed(function() {
            return ko.utils.arrayFilter(library, function(book) { 
                return book.language() === lang; 
            });
        });
    };
}

The findByLanguage method filters the array by language. In the view, im tryign to implement that like this:

<ul class="dropdown-menu">
    <li><a tabindex="-1" href="#" data-bind="click: findByLanguage('C')">C</a></li>
    <li><a tabindex="-1" href="#" data-bind="click: findByLanguage('Cpp')">C++</a></li>
</ul>

I’m attemping to reuse the function by calling the language parameter from there. But if I pass a function with parenthesis on data-bind it automatically gets called.

How can I accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:43:42+00:00Added an answer on June 13, 2026 at 8:43 pm

    You might be interested in a slightly different approach that add extra fields into your View Model.

    http://jsfiddle.net/jearles/RN9Dw/

    By adding languages into the View Model you can use Knockout to render the menu and the click binding will automatically pass the language that was clicked to the handler function. Additionally, adding selectedLanguage as an observable allows the books computed to change when you select or clear a language.

    HTML

    <ul class="dropdown-menu" data-bind="foreach: languages">
        <li><a tabindex="-1" href="#" data-bind="text: $data, click: $root.filterByLanguage"></a></li>
    </ul>
    <button data-bind="click: showAll">Show All</button>
    
    <div data-bind="foreach: books">
        <p><span data-bind="text: name"></span>, <span data-bind="text: language"></span></p>
    </div>​
    

    JS

    function BooksViewModel() {
        var self = this;
    
        self.languages = ko.observableArray(['C', 'C++']);
        self.selectedLanguage = ko.observable();
        self.library = [{name: 'Book A', language: 'C'}, {name: 'Book B', language: 'C++'}]; 
        self.books = ko.computed(function() {
            return ko.utils.arrayFilter(self.library, function(book) { 
              return self.selectedLanguage() == null ||
                  book.language === self.selectedLanguage(); 
              })
        });
    
        self.showAll = function() {
            self.selectedLanguage(null);
        }
        self.filterByLanguage = function(lang) {
            self.selectedLanguage(lang);
        };
    }
    
    ko.applyBindings(new BooksViewModel());
    

    ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little rake task: namespace :db do namespace :test do task :reset
I have this little problem, that I cannot figure out which arguments to pass
I have this little question that's been on my mind for a while now.
I have this little problem. My client wanted two distinct swf on a web
I have this little NetStream movie (which I'm not allowed to show, sorry) set
I have this little text <div> Hello world<br> this is a good<br> text! <br><br>
I have this little script that shows one wisdom each day. so I have
have written this little class, which generates a UUID every time an object of
I have this nice little MSBuild-based daily build setup that I use on my
I have this easy little form in my JavaFX application. I want to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.