AmpersandJS / ampersand-state

Core state management object.
MIT License
141 stars 75 forks source link

Derived property depending on other derived properties #216

Closed agentS closed 9 years ago

agentS commented 9 years ago

Hi,

I am currently developing an application depending on a derived property to enable or disable a button. However, this derived property depends on an amount of other derived properties which depend on regular properties. For debugging purposes I added an output line in the top level property (the one relying on the derived ones) which shows the returned value and this is working as demanded. However, listening for changes does not work, although it works when I exchange the property depending on the derived properties with a property calculated when a regular property changes.

I hope the following source code supports my explanation:

props:
{
    a: [ "number" ],
    b: [ "number" ]
},
derived:
{
    isValid:
    {
        deps:
        [
            "isAValid",
            "isBValid"
        ],
        fn: function()
        {
            console.log((this.isAValid && this.isBValid)); // works fine
            return (this.isAValid && this.isBValid);
        }
    },
    isAValid:
    {
        deps: [ "a" ],
        fn: function()
        {
            var checkResult = ...;// validity check
            return checkResult;
        }
    },
    isBValid:
    {
        deps: [ "b" ],
        fn: function()
        {
            var checkResult = ...;// validity check
            return checkResult;
        }
    }
}
...
this.model.on("change:isValid", ...) // doesn't work
this.model.on("change:isAValid", ...) // works
kamilogorek commented 9 years ago

Add cache: false to your derived properties that has to be checked every time.

agentS commented 9 years ago

Thanks a lot, this works great!

Am 2015-11-12 um 12:12 schrieb Kamil Ogórek:

Add cache: false to your derived properties that has to be checked every time.


Reply to this email directly or view it on GitHub: https://github.com/AmpersandJS/ampersand-state/issues/216#issuecomment-156076715

kamilogorek commented 9 years ago

Glad I could help. Cheers!