You can make this a bit less verbose by using the ||
trick twice.
value= ( (data || {}).title || "" )
题
I have a jade template that I would like to use for both inserts and updates of records. In order to accomodate updates I have setup my template like below:
label(for="title") Title
input.col-sm-10.form-control#title(type="text",placeholder="Enter title...", value=data.title)
Which works great for updates. But if I'm creating a new record then data
is null and the template errors out when trying to load. In order to get around this I have been using the following method:
label(for="title") Title
input.col-sm-10.form-control#title(type="text",placeholder="Enter title...", value= typeof data === 'undefined' || data === null ? "" : data.title)
Which works but seems very verbose. Is there a better method for handling this or do I need to add a ternary if statement if I want to use this view for both inserts and updates?
解决方案
You can make this a bit less verbose by using the ||
trick twice.
value= ( (data || {}).title || "" )