0

I have a computed observable that looks like this:

this.isActive = ko.computed<boolean>(function () {
    this.structure().valueSets();
    return this.structure().containsValueSet(this.valueSet());
}, this);

this.structure() is an instance of a StructureVM class (I'm using TypeScript). The containsValueSet function inside the StructureVM class looks like this:

containsValueSet(valueSetVM:ValueSetVM):boolean {
    var valueSet:ValueSetVM = _.find(this.valueSets(), function (valueSet:ValueSetVM) {
        if (valueSet.id === valueSetVM.id) {
            return true;
        }
    });
    return (Objects.isInstantiated(valueSet));
}

I'm using _.find on a list of ValueSetVM (this.valueSets()).The computed does not get updated when something is added to the valueSets array. Why is that? I'm assuming there is something wrong with the dependency chain?

Is it due to my usage of underscore?

Kind Regards,

DenEwout.

DenEwout
  • 885
  • 2
  • 10
  • 17
  • 2
    How are you adding things to the array? Are you calling `.push` etc on the actual `observableArray`, or on the underlying native javascript array? – James Thorpe Jul 14 '15 at 09:47
  • That turns out to be exactly my issue, a stupid mistake, I was looking way too far. Thank you! – DenEwout Jul 14 '15 at 10:41

1 Answers1

0

I needed to make sure to use .push on the observable array instead on the native javascript array. To give an example, I should have used this:

structure().valueSets.push(valueset);

instead of:

structure().valueSets().push(valueset);

Thanks to James Thorpe for reminding me.

DenEwout
  • 885
  • 2
  • 10
  • 17