javascript - Inline condition - Stack Overflow

- if (typeof(person) == 'undefined')input(type="text", name="person[Name]"

  - if (typeof(person) == 'undefined')
    input(type="text", name="person[Name]")
  - else
    input(type="text", name="person[Name]", value="#{person.Name}")

Is there any way to write this inline? I have an option select and I don't want to do a conditional statement for 30+ values to select the right option.

  - if (typeof(person) == 'undefined')
    input(type="text", name="person[Name]")
  - else
    input(type="text", name="person[Name]", value="#{person.Name}")

Is there any way to write this inline? I have an option select and I don't want to do a conditional statement for 30+ values to select the right option.

Share Improve this question edited Nov 2, 2018 at 23:51 A-Sharabiani 19.4k22 gold badges128 silver badges138 bronze badges asked Dec 19, 2011 at 16:10 PatrickPatrick 8,09312 gold badges55 silver badges91 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You could use mixins

mixin safeInput(person, property)
  - if (typeof(person) == 'undefined')
    input(type="text", name="person[#{property}]")
  - else
    input(type="text", name="person[#{property}]", value="#{person[property]}")

Then

mixin safeInput(person, 'Name')
mixin safeInput(person, 'Email')
...

conditional statement should do

input(type='text', name='person[Name]', value= (person?(person.name?person.name:''):''))

however, by design we can always pass a person? this way there is no parison required. Code would be something like

input(type='text', name='person[Name]', value= person.name)

You can short circuit as follows:

input(type="text", name="person[Name]", value="#{person && person.Name}")

When the value is undefined or null, the attribute will not be shown. This should work:

input(type='text', name='person[Name]', value= person && typeof(person))

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743716875a4495141.html

相关推荐

  • javascript - Inline condition - Stack Overflow

    - if (typeof(person) == 'undefined')input(type="text", name="person[Name]"

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信