0

I currently have a somewhat complex data model to work with. I have a group of dynamically-created computed observables that have to subscribe to a group of dynamically-created observables (ie, if any one of those observables changes, the whole computed has to refresh).

I'm scratching my head trying to solve this. The only knockout-ish solution I came up with was to make an observable group and a computed group (extending the respective observable). The computed group could subscribe to an observable, and it would emit the observables events to its enclosed computeds. Likewise, an observable contained in an observable group would emit its events to the parent, which would pass that on to its subscribers.

I don't think that's a very good idea, honestly, but it seems like it could work.

Is there a way to do this presently?

As an example, say I have a dynamic number of "investors" with a dynamic number of "priorities" attached to them, and a dynamic number "users" that calculate how many times they appear in any investor priority, so we have something like this:

These are the "users"

The above image represents the users, and how many times they appear in the investor list. These would be the computed observables.

These are the "investors"

The above image represents the investors. These are the observables that are subscribed to.

There is some extra information that goes into creating these fields, so many of them are created like this (this applies to both computeds and normal observables):

Here is the markup behind the investor rows.

<!-- ko foreach: new Array($root.MaxPriority()) -->
    <td>

        <select data-bind="options: $root.Users, 
                                    optionsText: 'FirstAndLastName', 
                                    optionsValue: 'UserID', 
                                    value: $root.getUserAtPosition($parent.InvestorID, $index() + 1)
                                    "></select>

    </td>
<!-- /ko -->

This is the function that creates the observable for the user (on the select box).

self.getUserAtPosition = function (invId, pos) {
    var uniqueSelectInst = ko.observable(function () {
        return 0;
    });

    return uniqueSelectInst;
};

Here is the markup that creates the users on the left.

<!-- ko foreach: Users.slice(1) -->
<tr>
    <td data-bind="text: FirstAndLastName"></td>
    <td><input type="checkbox" /></td>
    <td data-bind="text: $parent.countAppearancesForUser"></td>
</tr>
<!-- /ko -->

And here is the corresponding JS.

self.countAppearancesForUser = function (priority) {
    return ko.computed(function () {
        //code
    });
};

In fiddle form: http://jsfiddle.net/8U9M4/3/

I don't know how good this approach is, and I'm open to anything that would make it better.

Seiyria
  • 2,112
  • 3
  • 25
  • 51
  • 1
    Could you show a sample of what some of your computeds and observables look like so we can understand the relationship better? – PW Kad Aug 16 '13 at 18:56
  • Sure, will add that in a minute or so. – Seiyria Aug 16 '13 at 19:01
  • 1
    @Jeroen I've added a bunch of code. Hopefully it helps more. – Seiyria Aug 16 '13 at 19:49
  • It helps, but I'm sorry, I've reread your question several times and the context nor the question makes much sense to me. I'd like to help, perhaps you could add a [jsfiddle](http://jsfiddle.net) with your current solution and clarify your actual question ("don't know how good this approach is?" is rather broad). – Jeroen Aug 16 '13 at 21:15
  • @Jeroen Sorry for the late response (back in work today), I've added a JSFiddle with what I'm trying to do. Updating any select menu on the right should increase the count on the left. Hopefully this is more helpful. If not, I can clarify further. Thanks for your patience! – Seiyria Aug 21 '13 at 13:19
  • I'm looking at using [this approach](http://stackoverflow.com/questions/13769481/knockout-js-force-a-computed-to-run) to solve this problem now. I'm still not sure if that's a 'knockout' way to do it, but it will probably solve the problem (and I'm still open for a 'knockout' way to solve it). – Seiyria Aug 21 '13 at 14:33

1 Answers1

0

Just for closure, I want to add that I solved this using dummy computables, as seen here.

Community
  • 1
  • 1
Seiyria
  • 2,112
  • 3
  • 25
  • 51