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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:37:02+00:00 2026-05-31T04:37:02+00:00

Here is a weird race condition happening with knockoutjs. I’m setting two observables independantly

  • 0

Here is a weird race condition happening with knockoutjs. I’m setting two observables independantly using ajax calls. One is a list, the other is a single value. The weird thing is when I load the single value before the list, it won’t bind correctly. Any suggestions?

JsFiddle: http://jsfiddle.net/JasonMore/bxfXd/110/

View

    <form data-bind='submit:addItem'>
        Add item: <input data-bind='value:itemToAdd, valueUpdate: "afterkeydown"' type='text' />
        <button data-bind='enable: isAddButtonEnabled' type='submit'>Add</button>
    </form>

    <p>Your values:</p>
    <select data-bind='options:allItems, value:selectedItems' height='5'> </select>

    <div>
        <button data-bind='click: removeSelected'>Remove</button>
        <button data-bind='click: function() { allItems.sort() }, enable: allItems().length > 1'>Sort</button>
    </div>

</div>

Code

var betterListModel = function() {
    var self = this;

    // properties
    this.itemToAdd = new ko.observable("");
    this.allItems = new ko.observableArray();
    this.selectedItems = new ko.observable('');

    // computed
    this.isAddButtonEnabled = ko.computed(function() {
        return self.itemToAdd().length > 0
    });

    //methods
    this.addItem = function() {
        if ((this.itemToAdd() != "") && (this.allItems.indexOf(this.itemToAdd()) < 0)) this.allItems.push(this.itemToAdd());
        this.itemToAdd("");
    }

    this.removeSelected = function() {
        this.allItems.removeAll(this.selectedItems());
        this.selectedItems();
    } };

var view = new betterListModel();

ko.applyBindings(view);


// load $.ajax({
    url: '/echo/json/',
    type: 'post',
    data: {
        json: $.toJSON("Ham"),
        delay: 1
    },
    success: function(data) {
            view.selectedItems(data);
    } });

$.ajax({
    url: '/echo/json/',
    type: 'post',
    data: {
        json: $.toJSON(["Fries", "Eggs Benedict", "Ham", "Cheese"]),
        delay: 2
    },
    success: function(data) {
        $.each(data, function(index, value) {
            view.allItems.push(value);
        });
    } });
  • 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-31T04:37:03+00:00Added an answer on May 31, 2026 at 4:37 am

    Try this–>
    // Whenever the states changes, reset the selectedState selection

    this.allItems.subscribe(function () {
    this.selectedItems(arrayOfMySelectedItems);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running a weird issue here. I have code that makes jquery ajax calls
Have a really weird one here. The app has two targets, just to be
Weird to describe but here goes. I have a table with several rows. One
This is a weird one, but hopefully someone can give me an idea here.
I'm having a bit of a weird issue here, basically, im using hibernate together
Here is a weird one. I have an auto-hiding toolbar, which worked perfectly, until
So here's a weird one. I've been doing a combination of interface builder and
I got something weird happening here and I can't understand why, on my php
Got a weird one here. Site is deployed into production and trying to bring
i am experiencing some weird behaviour here. i am using the following code to

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.