Kidada
Kidada

Reputation: 225

If Else Error in CoffeeScript

I am new to CoffeeScript and I am not sure why this code is not evaluating properly. I am getting SyntaxError: unexpected ELSE. What I am trying to do is dynamically update the rule_severity drop down based on the selection of the rule_log_type drop down.

jQuery ->
sev = $('#rule_severity').html()
$('#rule_log_type').change ->
    logtype = $('#rule_log_type :selected').text()
    if (logtype == 'Syslog')
        $('#rule_severity')
        .find('option')
        .remove()
        .end()
        .append('<option value="Emergency">Emergency</option>')
        .append('<option value="Alert">Alert</option>')
        .append('<option value="Critical">Critical</option>')
        .append('<option value="Error">Error</option>')
        .append('<option value="Warning">Warning</option>')
        .append('<option value="Notice">Notice</option>')
        .append('<option value="Information">Information</option>')
        .val('Information')
    else if (logtype == 'Microsoft Windows')
        $('#rule_severity')
        .find('option')
        .remove()
        .end()
        .append('<option value="Critical">Critical</option>')
        .append('<option value="Warning">Warning</option>')
        .append('<option value="Information">Information</option>')
        .val('Information')

Upvotes: 0

Views: 803

Answers (3)

Kidada
Kidada

Reputation: 225

Working code. I think indentation is very key in CoffeeScript.

JQuery ->
sev = $('#rule_severity').html()
$('#rule_log_type').change ->
    logtype = $('#rule_log_type :selected').text()
    switch logtype
        when 'Syslog'
            console.log(logtype)
            $('#rule_severity')
            .find('option')
            .remove()
            .end()
            .append('<option value="Emergency">Emergency</option>')
            .append('<option value="Alert">Alert</option>')
            .append('<option value="Critical">Critical</option>')
            .append('<option value="Error">Error</option>')
            .append('<option value="Warning">Warning</option>')
            .append('<option value="Notice">Notice</option>')
            .append('<option value="Information">Information</option>')
            .val('Information')
        when 'Microsoft Windows'
            console.log(logtype)
            $('#rule_severity')
            .find('option')
            .remove()
            .end()
            .append('<option value="Critical">Critical</option>')
            .append('<option value="Warning">Warning</option>')
            .append('<option value="Information">Information</option>')
            .val('Information')

Upvotes: 1

Arie Xiao
Arie Xiao

Reputation: 14082

CoffeeScript use indentation to identify the scope of functions, control blocks, parameter list, etc. The code you pasted contains no syntax error except that you passed a empty function to jQuery (which is the same as $(document).ready. It's more likely that the following code are all contents of the document.ready handler, because you can only manipulate the DOM contents when they are ready.

jQuery ->
  sev = $('#rule_severity').html()
  $('#rule_log_type').change ->
    logtype = $('#rule_log_type :selected').text()
    if (logtype == 'Syslog')
      $('#rule_severity')
        .find('option')
        .remove()
        .end()
        .append('<option value="Emergency">Emergency</option>')
        .append('<option value="Alert">Alert</option>')
        .append('<option value="Critical">Critical</option>')
        .append('<option value="Error">Error</option>')
        .append('<option value="Warning">Warning</option>')
        .append('<option value="Notice">Notice</option>')
        .append('<option value="Information">Information</option>')
        .val('Information')
    else if (logtype == 'Microsoft Windows')
      $('#rule_severity')
        .find('option')
        .remove()
        .end()
        .append('<option value="Critical">Critical</option>')
        .append('<option value="Warning">Warning</option>')
        .append('<option value="Information">Information</option>')
        .val('Information')

Upvotes: 0

Zippie
Zippie

Reputation: 6088

It should probably be a else if instead of a else.

else if logtype == 'Microsoft Windows'

Upvotes: 1

Related Questions