Ruby on rails 重构咖啡脚本方法
我将此方法组合在一起,以在rails应用程序中实现一些基本验证 我对rails/coffeescript非常陌生,不知道是否有人对重构/简化rails/coffeescript有想法:Ruby on rails 重构咖啡脚本方法,ruby-on-rails,coffeescript,Ruby On Rails,Coffeescript,我将此方法组合在一起,以在rails应用程序中实现一些基本验证 我对rails/coffeescript非常陌生,不知道是否有人对重构/简化rails/coffeescript有想法: validateBillingAddress: (event) -> add1 = $('#user_billing_address_1') city = $('#user_billing_city') zip = $('#user_billing_zip') add1
validateBillingAddress: (event) ->
add1 = $('#user_billing_address_1')
city = $('#user_billing_city')
zip = $('#user_billing_zip')
add1.removeClass('error')
city.removeClass('error')
zip.removeClass('error')
if !$('#user_billing_agreement').is(':checked')
$('button[type=submit]').removeAttr('disabled')
alert('You must agree to the subscription')
return
if !add1.val().length
$('button[type=submit]').removeAttr('disabled')
add1.addClass('error')
return
else if !city.val().length
$('button[type=submit]').removeAttr('disabled')
city.addClass('error')
return
else if !zip.val().length
$('button[type=submit]').removeAttr('disabled')
zip.addClass('error')
return
else
@processCard()
event.preventDefault()
我想你可以试试这样的(未经测试)
我想你可以试试这样的(未经测试) 这是我的看法 非常小的单关注点函数。这可能有些过分,但我发现大多数应用程序的复杂性都会增加,而尽早获得一个干净的结构有助于以后的工作 此外,它还为以后在其他表单上重用此代码奠定了基础。甚至可能去上咖啡脚本课
#
# Handle submit event, validate, and submit
#
$('form').on "submit", ->
if isValidBillingAddress() and isAgreementChecked()
@processCard()
#
# Validation checks
#
isAgreementChecked = ->
if $('#user_billing_agreement').is(':checked')
true
else
alert('You must agree to the subscription')
false
isValidBillingAddress = ->
enableForm()
for field in requiredFormFields()
do (field) ->
if isInvalidData(field)
showErrorOnField(field)
else
field.removeClass('error')
#
# Support functions
#
requiredFormFields = ->
address1 = $("#user_billing_address_1")
city = $("#user_billing_city")
zip = $("#user_billing_zip")
[address1, city, zip]
isInvalidData = (field) ->
field.val() is ""
showErrorOnField = (field) ->
field.addClass('error')
enableForm = ->
$('button[type=submit]').removeAttr('disabled')
这是我的看法
非常小的单关注点函数。这可能有些过分,但我发现大多数应用程序的复杂性都会增加,而尽早获得一个干净的结构有助于以后的工作
此外,它还为以后在其他表单上重用此代码奠定了基础。甚至可能去上咖啡脚本课
#
# Handle submit event, validate, and submit
#
$('form').on "submit", ->
if isValidBillingAddress() and isAgreementChecked()
@processCard()
#
# Validation checks
#
isAgreementChecked = ->
if $('#user_billing_agreement').is(':checked')
true
else
alert('You must agree to the subscription')
false
isValidBillingAddress = ->
enableForm()
for field in requiredFormFields()
do (field) ->
if isInvalidData(field)
showErrorOnField(field)
else
field.removeClass('error')
#
# Support functions
#
requiredFormFields = ->
address1 = $("#user_billing_address_1")
city = $("#user_billing_city")
zip = $("#user_billing_zip")
[address1, city, zip]
isInvalidData = (field) ->
field.val() is ""
showErrorOnField = (field) ->
field.addClass('error')
enableForm = ->
$('button[type=submit]').removeAttr('disabled')
做得很好,它可以开箱即用<代码>除非是一个很棒的功能。另外,
filter
是jquery还是coffeescript函数?另外,如果您只需要检查字段是否为空,您可以使用HTML5验证。做得很好,它可以开箱即用<代码>除非是一个很棒的功能。另外,filter
是jquery还是coffeescript函数?另外,如果您只需要检查字段是否为空,您可以使用HTML5验证。