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

  • Home
  • SEARCH
  • 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 8232037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:41:48+00:00 2026-06-07T17:41:48+00:00

I am creating a guest list app using Knockout.js, and so far things are

  • 0

I am creating a guest list app using Knockout.js, and so far things are going swimmingly. However I have a best-practices question. My app has several different types of objects: guests and tags among them. Guests can have multiple tags, and tags can have multiple guests. At different points in the app, I need to display both arrays individually. For example, I have a “Guests” view where one can see all the guests along with their associated tags, and I also have a “Tags” view where one can see all tags and their associated guests. At present, the code for me to add a tag to a guest looks something like this:

var tag = function(opts) {
  this.guests = ko.observableArray()

  // Other tag code here...
}

var guest = function(opts) {
  this.tags = ko.observableArray()
  // Other guest code here...

  var self = this

  this.addTag = function(tag) {
    self.tags.push(tag)
    tag.guests.push(self)
  }
}

I know there must be a better way of doing this kind of many-to-many relationship in Knockout other than updating each observableArray independently. This also leads to a kind of recursive app structure where a guest has a tags property/array which contains a tag, which has a guest property/array which contains a guest, which has a tags property… you get the picture.

Right now, the ViewModel structure is like so:

- Parent Object
  - Guests ObservableArray
    - Guest Object
      - Tag Object as property of Guest
  - Tags ObservableArray
    - Tag Object
      - Guest Object as property of Tag

So I guess my question is twofold: 1) Is there a better way to structure my ViewModel to avoid recursive arrays? and 2) how can I better use Knockout.js to update my ViewModel in a DRY manner, rather than updating both the tags array AND the guests array individually? 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-07T17:41:51+00:00Added an answer on June 7, 2026 at 5:41 pm

    There are probably other ways to do this, but this method has pretty minimal duplication, without sacrificing proper modeling. A server should have no trouble generating the data in this format.

    Here it is in a (crudely styled) fiddle. Note, clicking a tag or guest will cause the selections below it to update (the first is selected by default).

    Basically, store the relationships by id, and use computed array’s to represent associations. Here is a basic viewmodel:

    var ViewModel = function(guests, tags) {
        var self = this;
        self.guests = ko.observableArray(
            ko.utils.arrayMap(guests, function(i){ return new Guest(i); }
        ));
        self.tags= ko.observableArray(
            ko.utils.arrayMap(tags, function(i){ return new Tag(i); }
        ));
    
        self.selectedGuest = ko.observable(self.guests()[0]);
        self.selectedTag = ko.observable(self.tags()[0]);
    
        self.guestTags = ko.computed(function() {
            return ko.utils.arrayFilter(self.tags(), function(t) {
                return self.selectedGuest().tags().indexOf(t.id()) > -1;
            });        
        });
    
        self.tagGuests = ko.computed(function() {
            return ko.utils.arrayFilter(self.guests (), function(g) {
                return self.selectedTag().guests().indexOf(g.id()) > -1;
            });        
        });
    };
    

    UPDATE

    So I have made a new fiddle to demonstrate a different kind of mapping, but this code could easily co-exist with the above viewmodel; its only seperate for demonstration. Instead of working off selection, it offers a general lookup, so that any code can consume it. Below is the HTML from Tags (guests is symmetrical), and the guestMap function that was added to the viewmodel.

    You will note that the names are inputs now, so you can change the names and watch all the bindings stay up to date. Let me know what you think:

    <div>Tags
        <ul data-bind="foreach: tags">
            <li>
                <input data-bind="value: name, valueUpdate: 'afterkeydown'" />
                </br><span>Tags</span>
                <ul data-bind="foreach: guests">
                    <li><span data-bind="text: $parents[1].guestMap($data).name()"></span></li>
                </ul>
            </li>
        </ul>
    </div>
    

    self.guestMap = function(id) {
            return ko.utils.arrayFirst(self.guests(), function(g) {
                return id == g.id();
            });
        };   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This app is a learning exercise for me using fragments, among other things. It
So I have an interesting design question regarding an app I'm developing for the
I am creating a word list searcher in C for my program using sqlite3
I'm creating a web app using Spring 3 and would like to harness the
I am creating a database table that'll have a list of all Tags available
I have created a django application. Using this app users can be created or
I'm creating the web app using Pyramid-1.2.1 with SQLAlchemy as database backend. Now I
I'm creating an app using PhoneGap and JQuery Mobile where I expect to be
I'm creating a web app that requires registration/authentication, and I'm considering using an email
Creating an a app, where I store bunch of photos in the drawable folder,

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.