简体   繁体   中英

How can I handle a potentially pre-filled form with Jade?

I have this huge form that only gets pre-filled data if data already exists in a database. Otherwise, none of the text boxes should have the value parameter in them. I thought it would just ignore the value parameters if the variable I get data from does not exist. But instead, I get an error.

How to I handle this case? Will I have to do an if check before each text box in Jade like the following?

if (typeof(prefilled_data) !== 'undefined')
    input.form-control#prevalence(type="text", name="prevalence")
else
    input.form-control#prevalence(type="text", name="prevalence", value=prefilled_data.tb_burden_estimates.prevalence)

While I don't mind doing this (Sublime Text will help with all the copy-pasting), there are quite a few form fields, so it might get ugly. Is there a way to consolidate this into one check somewhere?

you seemed to be suggesting that the if statement's were going to be bulky/make the code hard to read if they were there.. my suggestion would be to programmatically create the inputs, there by reducing the if statements to a more manageable number and answering your question about being able to "consolidate this into one check somewhere"

EDIT

If you are looking to access data in js.. I have been known to use something like:

script(type='text/javascript').
    window.prefilled_data = !{ JSON.stringify(prefilled_data) };

This will allow you then to access the global window.prefilled_data variable to get front end version of your data

you can do this:

- if (typeof(prefilled_data) === 'undefined'){
-   prefilled_data = '';
- }
input.form-control#prevalence(type="text", value=#{prefilled_data})

if prefilled_data is undefined you just set a '' value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM