Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally compute field only on attribute get #38

Open
jd-boyd opened this issue Apr 17, 2017 · 1 comment
Open

Optionally compute field only on attribute get #38

jd-boyd opened this issue Apr 17, 2017 · 1 comment

Comments

@jd-boyd
Copy link

jd-boyd commented Apr 17, 2017

I am using backbone-computedfields for a referenced object. However, I am loaded the referenced object on demand, it isn't a pre-loaded set.

I would like for the computed field to not be calculated until its attributed is gotten with a get call. I picture this being set with a lazy key in the options dictionary for the computed field.

    computed: {
        grossPrice: {
            depends: ['netPrice', 'vatRate'],
            get: function (fields) {
                return fields.netPrice * (1 + fields.vatRate / 100);
            },
            lazy: true
    }
@JSteunou
Copy link
Contributor

JSteunou commented May 23, 2017

You can already to it by using a function as dependency

In this function listen to when netPrice is set then listenTo its change.

Or maybe you could just set a default value for netPrice (null or undefined) and make grossPrice be null or undefined accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants