The Mystery of the Disappearing Checkmarks

前端 未结 1 1523
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 13:11

If a user clicks a checkbox the below code will fire, but the checkmark will sometimes disappear, the box is therefore unchecked when it should be checked.

$(doc         


        
1条回答
  •  悲&欢浪女
    2021-01-23 13:45

    Ok, tracked this nasty one down!

    The problem lays in your HTML generated. It turns out, that the problematic ones end up performing AJAX calls to... invalid URLs (causing 404's)!

    In your show view, you have code like:

    <% if @habit.current_level_strike %> 
      
    <% else %>
    <% end %> <% if @habit.current_level_strike %> <% else %> <% end %>

    Why is it problematic? Well, in your JavaScript, you're relying on exact classes of .habit-id and .level-id:

    habit = $(this).parent().siblings(".habit-id").first().attr("id");
    level = $(this).siblings(".level-id").first().attr("id");
    

    While according to HTML from show view, sometimes the proper classes are generated, and sometimes there are classes with appendix of *-two (habit-id-two and level-id-two).

    If you try fixing the class names, so all are of the same form expected by your JavaScript (.siblings(".habit-id") and .siblings(".level-id")), the problem disappears.

    Better solution (yes, it is possible to simplify it a bit ;))

    What if we pregenerate urls, and set them in HTML like so:

    <% @habit.levels.each_with_index do |level, index| %> <% if @habit.current_level >= (index + 1) %>

    <% end %> <% end %>

    Then, your JavaScript can be simplified to:

    $(document).on("page:change", function() {
      $(".habit-check").change(function()
      {
        var submitUrl = $(this).parents("p").data("submit-url");
        var deleteUrl = $(this).parents("p").data("delete-url");
    
        if($(this).is(":checked"))
        {
           $.ajax(
           {
             url: submitUrl,
             method: "POST"
           });
        }
        else
        {
           $.ajax(
           {
             url: deleteUrl,
             method: "DELETE"
           });
        }
      });
    });
    

    Please, be warned, that when generating delete-url, I've used hardcoded value of id, which is 1 (trying to reproduce your original behaviour), in:

    data-delete-url="<%= habit_level_days_missed_path({ habit_id: @habit.id, level_id: level.id, id: 1 }) %>"
    

    which corresponds to:

    url: "/habits/" + habit + "/levels/" + level + "/days_missed/1"
    

    in your code. Are you 100% sure this is what you want?

    Hope that helps! If you have any questions - I'm more than happy to help/explain!

    Good luck!

0 讨论(0)
提交回复
热议问题