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 9018239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:28:30+00:00 2026-06-16T04:28:30+00:00

I have a observable defined inside a object literal of a knockout viewmodel .

  • 0

I have a observable defined inside a object literal of a knockout viewmodel . Now when I ran the application .Its unable to access the observable .

$(function () {
  var viewModel =    {
    Folders: ['Inbox', 'Archive', 'Sent', 'Spam'],
    Title: ko.observable("My View Model Test"),
    SelectedFolder: ko.observable(),
    Mails: ko.observableArray(),
    SelectedMail: ko.observable(),
    SelectedChoices: ko.observable(false),


    navigate: function (folder) {           
        SelectedFolder(folder);
        $.ajax({
            url: "/Api/MailBox",
            data: { folder: folder },
            success: function (data) {
                self.Mails(data);
            },
            statusCode: {
                404: function () {
                    console.log("No Mails");
                }
            }

        });
    }
};
}

when I have bind the click event to navigate function . It says SelectedFolder is undefined . Can someone tell me why is it unable to access the SelectedFolder observable inside the navigate function ?

  • 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-16T04:28:31+00:00Added an answer on June 16, 2026 at 4:28 am

    When the navigate method is running and it looks for the SelectedFolder observable, it first looks for it in the context of the navigate method. Failing that, it jumps up to the parent context, which is the anonymous function being run when the page is ready. Failing that, it jumps to the global context – it can’t find SelectedFolder there, so it then gives up.

    To fix this, change your navigate method to reference the viewModel variable that is available in the context of the anonymous function being run when the page is ready:

    navigate: function (folder) {           
      viewModel.SelectedFolder(folder);
      $.ajax({
        url: "/Api/MailBox",
        data: { folder: folder },
        success: function (data) {
          viewModel.Mails(data);
        },
        ...
    

    Note that I also added a reference to the viewModel in the success callback so that it could find the Mails observable.

    This should work, however, I would recommend considering changing your structure so that your navigate method isn’t dependent on specific global variables. Here is one way:

    var myViewModel = function() {
        var self = this;
        self.Folders = ['Inbox', 'Archive', 'Sent', 'Spam'];
        self.Title = ko.observable("My View Model Test");
        self.SelectedFolder = ko.observable();
        self.Mails = ko.observableArray();
        self.SelectedMail = ko.observable();
        self.SelectedChoices = ko.observable(false);
    
        self.navigate = function (folder) {           
            self.SelectedFolder(folder);
            $.ajax({
                url: "/Api/MailBox",
                data: { folder: folder },
                success: function (data) {
                    self.Mails(data);
                },
                statusCode: {
                    404: function () {
                        console.log("No Mails");
                    }
                }
    
            });
        };
    };
    
    $(function () {
      var ViewModel = new myViewModel();
      ko.applyBindings(viewModel);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following object defined function BusinessUnit(id, name, code) { this.id = ko.observable(id);
I have a Knockout view model defined like this: function viewModel () { var
in my Silverlight 4 Application, I have an observable list of a user defined
I have a knockoutjs viewmodel defined like this: function TestViewModel() { var self =
I have a wpf-mvvm application. I have an observable collection in my viewmodel public
i have Observable collection object bounded to a treeView: MainTreeView.ItemsSource = ((App)Application.Current).TucOC; the problem
In my viewmodel, I have defined: self.myAreasEditMode = ko.observable(false); In my html, I have:
I have just started with knockout this Model and viewmodel: $(function() { // Class
Right now I came across the Observer/Observable pattern where I have my Observable notify
I have a Knockout observable array that I wish to edit from within Javascript

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.