Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Vuex state object is empty

I have an initial empty object in Vuex state that gets updated from an API.

const state = {
  someObject: {}
}

How do I check if the object is empty in my template?

<template>
  <div v-if="someObject">
    This should not display when someObject is empty.
  </div>
</template>

What is best practice for checking if a state object is set/empty or not?

Should i set someObject: null/undefined/false initially, even if it expects to be updated with a new object?

Does it make sense to do a check in getters?

export const someObject = state => Object.getOwnPropertyNames(state.someObject).length == 0 ? state.someObject : false
like image 681
Andreas Avatar asked Jul 06 '17 21:07

Andreas


2 Answers

You could use the lodash method: _.isEmpty({someObject});

Or if you wanted to do a getter:

computed:{
  objectLength(state){
    return Object.keys(state.someObject).length
  }
like image 130
aprouja1 Avatar answered Oct 19 '22 19:10

aprouja1


Depending on particular use case I would ether set it to null/undefined or make if check by some required object property like v-if='someObject.id'

Anything else seams as unnecessary complication.

like image 27
snovakovic Avatar answered Oct 19 '22 20:10

snovakovic