vuejs / Discussion

Vue.js discussion
167 stars 17 forks source link

Vuejs - Deep nested computed properties #407

Open IntellectProductions opened 9 years ago

IntellectProductions commented 9 years ago

I'm not really understanding where to put function() { return {} } and where not to when it comes to deeply nesting computed properties.

BY THE WAY, this is in a component!

computed: {
        styles: function() {
            return {
                slider: function() {
                    return {
                        height: {
                            cache: false,
                            get: function() {
                                return 'auto';
                            }
                        },
                        width: {
                            cache: false,
                            get: function() {
                                return $('#slideshow').width();
                            }
                        }
                    }
                }
            }
        }
    },

This is returning undefined. When I get rid of the function() { return {} } inside of the slider index, it returns an object when I do styles.slider.width instead of the get() return. It just shows an object with cache and get as indexes..

Thanks for any help!

The reason I'm asking is because I have multiple nested components that involve styling from the parent. Slider, tabs, carousels, etc. So I wanted to organize them like this.

IntellectProductions commented 9 years ago

Anybody know about this?

simplesmiler commented 9 years ago

You need to function() { return { ... }; } only once for each property, thus making the computed a bag of functions that return plain objects.

computed: {
    styles: function() {
        return {
            slider: {
                height: 'auto',
                width: $('#slideshow').width(),
            },
        };
    },
},

Then you can use v-style="styles.slider".

IntellectProductions commented 9 years ago

I ended up doing what you suggested like so (I have to keep the cache: false in there because width will only return the initial value.. I'm doing a resize event so I have to have the non-cached value for when the window changes):

computed: {
    styles: function() {
        return {
            slider: {
                height: {
                    cache: false,
                    get: function() {
                        return 'auto';
                    }
                },
                width: {
                    cache: false,
                    get: function() {
                        return $('#slideshow').width();
                    }
                }
            }
        }
    }
},

And then in a child component, I call this:

console.log(this.$parent.styles.slider.width);

And it's returning this:

Object {cache: false}
cache: false
get: function()
__proto__: Object

It won't actually just return the value :/ It does that for both computed properties..

simplesmiler commented 9 years ago
computed: {
    styles: {
        cache: false,
        get: function() {
            return {
                slider: {
                    height: 'auto'
                    width: $('#slideshow').width(),
                },
            };
        },
    },
},