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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:26:18+00:00 2026-06-11T19:26:18+00:00

Issue : I have Json coming back from the server. The Json is an

  • 0

Issue: I have Json coming back from the server. The Json is an array of businesses where each business can have multiple contacts. I want to make the contacts an observable array also, so that html elements bound to the contacts get updated upon deletion and the array gets updated upon editing.

I’ve setup the following jsFiddle – http://jsfiddle.net/rdotlee/GCwjX/1/.

Here is my view model from the jsFiddle.

    var businessViewModel =
{
    allBusinesses: ko.observableArray([
                                        { name: "Biz1", id: 1, Contacts: [{ name: "Joe", email: "test@test", phone: "555-111-1111" }, { name: "Smith", email: "smith@test", phone: "777-111-2223"}] },
                                        { name: "Biz2", id: 2, Contacts: [{ name: "Joe", email: "test@test", phone: "555-222-1111" }, { name: "Smith", email: "smith@test", phone: "555-111-2222"}] }
                                      ]),

    businessId: ko.observable(1)

};

businessViewModel.selectedBusiness = ko.dependentObservable(function () {
    var biz = this.allBusinesses()[0];

    for (var i = 0; i < this.allBusinesses().length; i++) {
        if (this.allBusinesses()[i].id == this.businessId()) {
            biz = this.allBusinesses()[i];
            break;
        }
    }
    return biz;
}, businessViewModel);

businessViewModel.removeContact = function (contact) {
    ko.utils.arrayRemoveItem(this.selectedBusiness().Contacts, contact);
    ko.applyBindings(businessViewModel, $("#sectionBusinesses")[0]);
} .bind(businessViewModel);

What is the cleanest / recommended way to do what I need?

Thanks,

  • 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-11T19:26:19+00:00Added an answer on June 11, 2026 at 7:26 pm

    First off, I’m not sure why you’re calling ko.applyBindings every time a contact is removed. You’ll generally only ever want to call applyBindings once, any more than that usually causes problems.

    I would recommend using the ko.mapping plugin, which will make the whole process rather painless.

    I’ve also updated your removeContact to work with the updated ViewModel and changed your selectedBusiness a little too.

    Here’s the fiddle: http://jsfiddle.net/GCwjX/5/
    and the code:

    var jsonData = {
        allBusinesses: [{
            name: "Biz1",
            id: 1,
            Contacts: [{
                name: "Joe",
                email: "test@test",
                phone: "555-111-1111"},
            {
                name: "Smith",
                email: "smith@test",
                phone: "777-111-2223"}]},
        {
            name: "Biz2",
            id: 2,
            Contacts: [{
                name: "Joe",
                email: "test@test",
                phone: "555-222-1111"},
            {
                name: "Smith",
                email: "smith@test",
                phone: "555-111-2222"}]}]
    };
    var businessViewModel = {
        allBusinesses: ko.observableArray(),
        businessId: ko.observable(1)
    };
    
    businessViewModel.selectedBusiness = ko.dependentObservable(function() {
        var biz = this.allBusinesses()[0];
        biz = ko.utils.arrayFirst(this.allBusinesses(), function(item) { 
            return (item.id() === parseInt(this.businessId())); 
        }, this);
        return biz;
    }, businessViewModel);
    
    businessViewModel.removeContact = function(contact) {
        this.selectedBusiness().Contacts.remove(contact);
    }.bind(businessViewModel);
    
    ko.mapping.fromJS(jsonData, {}, businessViewModel);
    ko.applyBindings(businessViewModel);​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue regarding to auto populating a select dropdown from jQuery/JSON data
Hey friends can you help me out with this issue. I have a JSON:
I have parsed a JSON file then saved a array from the JSON to
I have been working on a json decode issue (which I have already had
I have the following issue: I have a splash like window coming with my
The issue I have is that I want to parse strings in order, so
I have a JSON format result sent back to the client that hold the
I have a REST service which takes a JSON request. I want to validate
I have an issue where if my json file looks like this { Numbers:
I am parsing json data. I don't have an issue with parsing and I

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.