0
votes

I use Breeze in my asp.net mvc project.

I defined the following computed function:

var isDetached = ko.computed(function () {
    if (rolling())
        return rolling().entityAspect.entityState.isDetached();
});

An entity is detached (for example) when it is first added and then (immediately) cancelled.

Basic scenario:

  • (1) create a new 'rolling'
  • >> the entityState is 'Added'
  • (2) cancel the operation of creation (datacontext.cancelChanges)
  • >> the entityState is 'Detached'

The problem: when trying to get the value of entityState.isDetached() from my computed knockout observable, the value doesn't seems to be dynamic. From the previous basic scenario, after step 1, the ko.computed isDetached() is false (because it is Added). After step 2, the ko.computed is still false but it should be true.

It works if I explicitly get the value like this:

if (rolling().entityAspect.entityState.isDetached())
    ...

Any idea why my ko.computed is not dynamic?

Thanks.

2

2 Answers

1
votes

A computed observable will only be reevaluated when Knockout detects that a value used in the computed observable has changed. Knockout can only detect changes in observables. If you use a variable inside your computed observable that is not an observable, and that variable changes its value, Knockout will not detect it.

for a computed observable to work, all variables used within it (that may change value) must be observables.

0
votes

aaberg is essentially correct. Of course you can include other variables in the computed but KO won't detect that they have changed ... and that is his point ... and the source of your problem.

The entityAspect and its properties are not observables. That is a design decision, not a casual omission.

I could recommend an approach if I understood how you were using the information. What should happen when the EntityState changes?