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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:16:52+00:00 2026-06-11T16:16:52+00:00

I have two functions generate and test . When the user clicks a button

  • 0

I have two functions generate and test. When the user clicks a button generate is called which then calls test function. I receive the following error:

Pass a function that returns the value of the ko.computed.

The error is thrown on this line in the test function.:

var unmapped = ko.mapping.toJS(this); 

this is my viewmodel with ko.observable properties.

Here are the two functions. If I move the content of test function to generate everything works fine but I need the logic to be in two different functions. What can I do?

I am very stuck here. Any help will be greatly apreciated.

generate = function () {
            if (!omega.validatableFranchiseGeneration.validate()) {
                return false;
            }
            omega.franchiseInfo.IsForGeneration(true);

            test();
        }

 var test = function () {
        getIpsAndPorts();

        for (var i = 0; i < omega.franchiseInfo.LanguagesInfoViewModel().length; i++) {
            if ($.isArray(omega.franchiseInfo.LanguagesInfoViewModel()[i].SubMenuItemsViewModel)) {
                omega.franchiseInfo.LanguagesInfoViewModel()[i].SubMenuItemsViewModel = omega.franchiseInfo.LanguagesInfoViewModel()[i].SubMenuItemsViewModel[0];
            }
        }

        var unmapped = ko.mapping.toJS(this);

        var jsonData = ko.toJSON(unmapped);
        $.ajax({
            url: "/franchise/Save",
            type: "POST",
            // data: ko.toJSON({ folderName: FolderName }),
            data: { franchiseInfoViewModel: jsonData },
            //traditional: true,
            //contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function (data, textStatus, xhr) {
                window.location.href = data.redirectToUrl;
            },
            error: function (request, status, error) {
                jsonValue = jQuery.parseJSON(request.responseText);
                omega.franchiseInfo.errorMessages([]);
                for (var i = 0; i < jsonValue.errorMessages.length; i++) {
                    omega.franchiseInfo.errorMessages.push({ errorMessage: ko.observable(jsonValue.errorMessages[i].ErrorMessage) });
                }
                for (var i = 0; i < omega.franchiseInfo.LanguagesInfoViewModel().length; i++) {
                    InitializeViewLanguagesInfo(omega.franchiseInfo.LanguagesInfoViewModel()[i]);
                }
            }
        });
    }   
  • 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-11T16:16:53+00:00Added an answer on June 11, 2026 at 4:16 pm

    Without seeing all your code this is hard to be sure, but if the code (as is) works when directly included in generate, then I’d be willing to bet that this is not what you think it is in the test function. this has a different meaning inside the test function than it did inside generate.

    Fire up Chrome or Firebug and set a breakpoint on the var unmapped = ko.mapping.toJS(this); line. Run your program and when the breakpoint hits go to the Console and look at this. Is it your ViewModel?

    If this is what you expect inside generate you could always call test like this:

    test.apply(this);
    

    This will explicitly set the context of this for the method invocation.

    Another option is to set a self variable inside your ViewModel. This is usually done at the top and looks like: var self = this;. By creating this variable you can then reference self' inside any functions within the outer "function" scope, and not have to worry about the value ofthis` fluctuating.

    —

    Here is a simple fiddle to simulate what I think is happening: http://jsfiddle.net/jearles/aLFWe/

    Open Chrome or Firebug console and look at the logged objects. Note that Window was the logged object when I just called updatea(), but was Object in the original click function and when I called updateb.apply(this) or updatec() (which referenced self).

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

Sidebar

Related Questions

So, I have two JavaScript functions: function Generate() { //Do something $.ajax({data:{Svc: 'cpMain', Cmd:
I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have two functions here function Preloader() {} Preloader.prototype = { init:function() { //
I have two functions, but one of the functions is only called from the
I have two functions in an ActionScript class, they are: private function loaderCompleteHandler(event:Event):void {
I have two functions hooked on the submit event of a form. Each function
I have two precompiled library: X.a and Y.a and a test.cpp (without main function)
I have two functions declared as following, using extern C aming to avoid name
I have two tables, custassets and tags . To generate some test data I'd
I have this code which generates me two images. What it does is generate

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.