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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:03:10+00:00 2026-05-18T20:03:10+00:00

I’m creating an app using the very slick KnockoutJS library, but I’ve run into

  • 0

I’m creating an app using the very slick KnockoutJS library, but I’ve run into a snag. On the html page, I have a plain <select> control that I want to load with JSON data returned from a web service.

I define the observable array as follows:

var laborRow = function () {
    this.positions = ko.observableArray([]);
};

When the page loads, the ajax call is made and the data is returned. In the callback, I do the following:

    success: function (msg) {
        laborRow.positions = msg;
    }

based on the KO docs, I would expect that I would set the result like this:

laborRow.positions(msg);

However, that just throws an error stating that “laborRow.positions in not a function”

The template in the html is as follows:

<tbody data-bind='template: {name: "laborRowTemplate", foreach: laborLine}'> </tbody> 
</div>
  <script type="text/html" id="laborRowTemplate"> 
        <tr>

          <td><select data-bind='options: positions, optionsText: "Title", optionsCaption: "select", value: selectedPosition '></select></td>

        </tr>
    </script>

The laborRow object is a property on the ViewModel which is bound to the page. For whatever reason, this does not work. To add another wrinkle, if I add code to peek into the observableArray and print out some piece of data, the data is in there. So it is being loaded successfully.

Any thoughts would be greatly appreciated.

The full code for my example case:

var laborRow = function () {
    this.positions = ko.observableArray([]);    
};

var projectEstimate = function () {
    this.laborLine = ko.observableArray([new laborRow()]);

};

var projectViewModel = new projectEstimate();
ko.applyBindings(projectViewModel);

//and the code in the callback function on ajax success

 success: function (msg) {
                laborRow.positions = msg;
                //laborRow.positions(msg); **this does not work - error is laborRow.positions is not a function**
            },

And the html:

 <tbody data-bind='template: {name: "laborRowTemplate", foreach:
laborLine}'> </tbody>

  <script type="text/html" id="laborRowTemplate">
        <tr>
          <td><select data-bind='options: positions, optionsText:
"Title",  optionsCaption: "select", value: selectedPosition '></
select></td>

        </tr>
    </script> 

Finally, thanks to Sean’s comments below, I was able to get it working by modifying the code in the callback as follows:

success: function (msg) {
    projectViewModel.laborLine()[(projectViewModel.laborLine().length-1)].positionList(msg);
}
  • 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-18T20:03:11+00:00Added an answer on May 18, 2026 at 8:03 pm

    The problem is that you haven’t actually created your model:

    var laborRow = function () {
        this.positions = ko.observableArray([]);
        // will only be called if you call var some_var = new laborRow()
    };
    

    Change your function to a bare object (as shown in the Knockout docs):

    var laborRow = {
        positions: ko.observableArray([])
    };
    

    And you’ll be able to call laborRow.positions(msg); and have it work.


    EDIT

    Based on the new code, laborRow is still not instantiated — if you are setting var laborRow somewhere else in your code (around the ajax request, perhaps) then you’ll want to make sure that your call stack looks like this:

    projectViewModel.laborLine()[0].positions() 
    // This will return the array you're looking for.
    // The key is that laborLine is a `getter` not an attribute
    

    I’ve been bitten by the “ko variables are getters not attributes” bug on several occasions … might that be happening with your code?

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

Sidebar

Related Questions

No related questions found

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.