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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:15:57+00:00 2026-06-04T22:15:57+00:00

Say I have two view models that each have an observable property that represents

  • 0

Say I have two view models that each have an observable property that represents different, but similar data.

function site1Model(username) {
    this.username = ko.observable(username);
    ....
}

function site2Model(username) = {
    this.username = ko.observable(username);
    ....
}

These view models are independent and not necessarily linked to each other, but in some cases, a third view model creates a link between them.

function site3Model(username) = {
    this.site1 = new site1Model(username);
    this.site2 = new site2Model(username);
    // we now need to ensure that the usernames are kept the same between site1/2
    ...
}

Here are some options that I’ve come up with.

  1. Use a computed observable that reads one and writes to both:

    site3Model.username = ko.computed({
        read: function() {
            return this.site1.username();    // assume they are always the same
        },
        write: function(value) {
            this.site1.username(value);
            this.site2.username(value);
        },
        owner: site3Model
    }
    

    This will keep the values in sync as long as changes always come through the computed. But if an underlying observable is changed directly, it won’t do so.

  2. Use the subscribe method to update each from the other:

    site3Model.site1.username.subscribe(function(value) {
        this.site2.username(value);
    }, site3Model);
    site3Model.site2.username.subscribe(function(value) {
        this.site1.username(value);
    }, site3Model);
    

    This works as long as the observables suppress notifications when the values are the same; otherwise you’d end up with an infinite loop. You could also do the check earlier: if (this.site1.username() !== value) this.site1.username(value); This also has a problem that the observables have to be simple (it won’t work right if site1 and site2 themselves are observables).

  3. Use computed to do the subscribe and updates:

    site3Model.username1Updater = ko.computed(function() {
        this.site1.username(this.site2.username());
    }, site3Model);
    site3Model.username2Updater = ko.computed(function() {
        this.site2.username(this.site1.username());
    }, site3Model);
    

    This format allows us to have other dependencies. For example, we could make site1 and site2 observables and then use this.site1().username(this.site2().username()); This method also requires a check for equality to avoid an infinite loop. If we can’t depend on the observable to do it, we could check within the computed, but would add another dependency on the observable we’re updating (until something like observable.peek is available).
    This method also has the downside of running the update code once initially to set up the dependencies (since that’s how computed works).

Since I feel that all of these methods have a downside, is there another way to do this that would be simple (less than 10 lines of code), efficient (not run unnecessary code or updates), and flexible (handle multiple levels of observables)?

  • 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-04T22:15:58+00:00Added an answer on June 4, 2026 at 10:15 pm

    Ryan and John, Thank you both for your answers. Unfortunately, I really don’t want to introduce a global naming system that the pub/sub systems require.

    Ryan, I agree that the subscribe method is probably the best. I’ve put together a set of functions to handle the subscription. I’m not using an extension because I also want to handle the case where the observables themselves might be dynamic. These functions accept either observables or functions that return observables. If the source observable is dynamic, I wrap the accessor function call in a computed observable to have a fixed observable to subscribe to.

    function subscribeObservables(source, target, dontSetInitially) {
        var sourceObservable = ko.isObservable(source) 
                ? source 
                : ko.computed(function(){ return source()(); }),
            isTargetObservable = ko.isObservable(target),
            callback = function(value) {
                var targetObservable = isTargetObservable ? target : target(); 
                if (targetObservable() !== value)
                    targetObservable(value);
            };
        if (!dontSetInitially)
            callback(sourceObservable());
        return sourceObservable.subscribe(callback);
    }
    
    function syncObservables(primary, secondary) {
        subscribeObservables(primary, secondary);
        subscribeObservables(secondary, primary, true);
    }
    

    This is about 20 lines, so maybe my target of less than 10 lines was a bit unreasonable. 🙂

    I modified Ryan’s postbox example to demonstrate the above functions: http://jsfiddle.net/mbest/vcLFt/

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

Sidebar

Related Questions

Say I have two classes and have a requirement that the primary key property
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Let's say I have an app that matches two People against each other, kind
I have a view table which is a union of two separate tables (say
Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they
Say I have a ListBox that is bound to an observable collection on my
Let's say I have two models, Book and Page: class Book(models.Model): pass class Page(models.Model):
I have a two pages say Main.xaml and Details.xaml .Each page has a ListBox
Requirement 1 Let's say I have some view model with two DateTime properties, Start
I have two table view controllers. Say TableViewController1 and TableViewController2 . I push TableViewController2

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.