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

  • SEARCH
  • Home
  • 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 7607389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:39:00+00:00 2026-05-31T00:39:00+00:00

I am a beginner to the Javascript MVC framework Knockout.js Coming from conventional Javascript

  • 0

I am a beginner to the Javascript MVC framework Knockout.js

Coming from conventional Javascript (and some jQuery experience), I am having difficulties in understanding the syntax learning Knockout.js

Consider the statments below;

The View:

<ul class="folders" data-bind="foreach: folders">
<li data-bind="text: $data, 
               css: { selected: $data == $root.chosenFolderId() },
               click: $root.goToFolder"></li></ul>

View Model:

function WebmailViewModel() {
    // Data
    var self = this;
    self.folders = ['Inbox', 'Archive', 'Sent', 'Spam'];
    self.chosenFolderId = ko.observable();

    // Behaviours    
    self.goToFolder = function(folder) { self.chosenFolderId(folder); };    
};

Could you please explain me what the statements do (specifically $data, $root) ?
Also what does the statement self.chosenFolderId(folder); does ?

  • 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-05-31T00:39:01+00:00Added an answer on May 31, 2026 at 12:39 am

    The $data and $root keywords are typically used by KO.
    When you use the foreach on array ( data-bind), KO creates one <li> for each element in the array.

    In this case, $data is the current item of array (like folders[i]) and $root is the parent element.
    For you, folders is a field of your ViewModel :
    $data = current folders on the iteration (viewModel.folder[i])
    $root = viewModel

    self.chosendFolderId(folder) execute you viewModel chosenFolderId method. The code use self to keep the viewModel value because in the function the keyword “this” isn’t the viewModel but the method’s sender. It’s a closure.

    Edit :
    The $parent key word is the tree’s previus level.
    The $root key word is the top tree’s level.

    viewModel {
      topObjects : ko.observableArray()
    }
    
    viewModel.topObjects.push({
      Objects : ko.observableArray()
    });
    

    If we create a foreach on viewModel.topObjects.Objects, the $parent is topObjects, $root is viewModel.

    Thanks Tjorriemorrie 😉

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

Sidebar

Related Questions

I am a COMPLETE beginner at JQuery (and javascript in general) but I'm having
I'm just starting to get into jquery/javascript programming so this may be a beginner
I have a beginner level Json question with MVC.net (I've never really used jquery
I'm a complete beginner when it comes to Jquery, Javascript etc but I've managed
I am very new to Javascript and Jquery, so my apologies for this beginner's
I'm a total beginner to JavaScript, jQuery, and everything else, so when I saw
I'm beginner in jquery.this is my first application. <head> <title>Untitled Document</title> <script type=text/javascript src=../jquery-1.6.2.min.js></script>
I'm a beginner with JS. I am working with some JavaScript on a site,
I am a total beginner with Javascript and JQuery, and am trying to achieve
So I'm very beginner with javascript and would love some help simplifying this code.

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.