I am working on an angular application where I have loaded site settings from an API, this api returns a json object something like following
"data":{
"merchant_settings": {
"admin": {
"def_referral_amount": 50,
"card_payment_mode": "test",
"min_amount": 150,
"payment_mode": true
},
"bookings": {
"allow_calendar_to_customer": {
"enable": "yes",
"layout": "month"
},
},
},
}
In the template file, I have section to be shown based on the data returned like if allow_calendar_to_customer.enable == yes then show a calendar to customer. this is working properly if I am getting data from API but sometimes it is possible that this data is not saved in db and hence it will not be returned in response. To handle this case, I have added conditions like following
if( typeof(data) !== 'undefined' &&
typeof(data.merchant_settings) !== 'undefined' &&
typeof(data.merchant_settings.bookings) !== 'undefined' &&
typeof(data.merchant_settings.bookings.allow_calendar_to_customer) !== 'undefined' &&
typeof(data.merchant_settings.bookings.allow_calendar_to_customer.enable) !== 'undefined' &&
data.merchant_settings.bookings.allow_calendar_to_customer.enable == yes){
/* show required content */
}
In this condition, I have to check each and every parent element which I think is not a good solution but I am also not sure what is the best way to handle this case, I was expecting that something like below will work but its not.
if( typeof(data.merchant_settings.bookings.allow_calendar_to_customer.enable) !== 'undefined' && data.merchant_settings.bookings.allow_calendar_to_customer.enable == yes){
/* show required content */
}
can someone suggest me a better way to implement these type of conditions so that I dont have to check all the attributes in object ?
Aucun commentaire:
Enregistrer un commentaire