我有一些coffeescript用于声明表单元素
validation = {}
if isRequired
validation.required =
params: true
message: "Please enter a year between #{minYear} & #{maxYear}."
@Year = new inputFormItem
validation: validation
是否可以对此进行内联声明?
像这样的东西,但我写的不编译
@Year = new inputFormItem
validation:
if isRequired then required: { params: true, message: "Please enter a year between #{minYear} & #{maxYear}." }
我不知道是否可以在对象声明中放入内联条件
如果需要,那么我需要该对象,否则我不需要所需的对象。
答案 0 :(得分:2)
试试这个:
@Year = new inputFormItem
validation:
required: if isRequired then { params: true, message: "Please enter a year between #{minYear} & #{maxYear}." } else null
默认为null
(如果需要,可以undefined
)。如果您希望默认为false
,则可以执行此操作:
@Year = new inputFormItem
validation:
required: isRequired and { params: true, message: "Please enter a year between #{minYear} & #{maxYear}." }