1. <tfoot id='7Pywj'></tfoot>
  2. <legend id='7Pywj'><style id='7Pywj'><dir id='7Pywj'><q id='7Pywj'></q></dir></style></legend>

    1. <i id='7Pywj'><tr id='7Pywj'><dt id='7Pywj'><q id='7Pywj'><span id='7Pywj'><b id='7Pywj'><form id='7Pywj'><ins id='7Pywj'></ins><ul id='7Pywj'></ul><sub id='7Pywj'></sub></form><legend id='7Pywj'></legend><bdo id='7Pywj'><pre id='7Pywj'><center id='7Pywj'></center></pre></bdo></b><th id='7Pywj'></th></span></q></dt></tr></i><div id='7Pywj'><tfoot id='7Pywj'></tfoot><dl id='7Pywj'><fieldset id='7Pywj'></fieldset></dl></div>

      <small id='7Pywj'></small><noframes id='7Pywj'>

        <bdo id='7Pywj'></bdo><ul id='7Pywj'></ul>

      CoffeeScript 总是以匿名函数返回

      时间:2024-04-20
    2. <i id='HSgKr'><tr id='HSgKr'><dt id='HSgKr'><q id='HSgKr'><span id='HSgKr'><b id='HSgKr'><form id='HSgKr'><ins id='HSgKr'></ins><ul id='HSgKr'></ul><sub id='HSgKr'></sub></form><legend id='HSgKr'></legend><bdo id='HSgKr'><pre id='HSgKr'><center id='HSgKr'></center></pre></bdo></b><th id='HSgKr'></th></span></q></dt></tr></i><div id='HSgKr'><tfoot id='HSgKr'></tfoot><dl id='HSgKr'><fieldset id='HSgKr'></fieldset></dl></div>

      1. <legend id='HSgKr'><style id='HSgKr'><dir id='HSgKr'><q id='HSgKr'></q></dir></style></legend>

              <tbody id='HSgKr'></tbody>
            <tfoot id='HSgKr'></tfoot>
                <bdo id='HSgKr'></bdo><ul id='HSgKr'></ul>

              • <small id='HSgKr'></small><noframes id='HSgKr'>

                本文介绍了CoffeeScript 总是以匿名函数返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I'm trying to write some CoffeScript function which checks all checkboxes in a table upon checking the checkbox in the th.

                My function in CoffeeScript looks like this:

                $("table.tableview th input:checkbox").live 'click', -> 
                  checkedStatus = this.checked
                  $("table.tableview tbody tr td:first-child input:checkbox").each ->
                      this.checked = checkedStatus
                

                It works great for checking all the boxes. However when unchecking it doesn't work. The compiled JS looks like this:

                $("table.tableview th input:checkbox").live('click', function() {
                  var checkedStatus;
                  checkedStatus = this.checked;
                  return $("table.tableview tbody tr td:first-child input:checkbox").each(function() {
                    return this.checked = checkedStatus;
                  });
                });
                

                It doesn't work because after the first one is set to false the return of the function will be false. I however have no clue how to suppress this default return behavior of coffee script. Please help.

                When I add a "true" as per Flambino's suggestion I get the following JS

                $("table.tableview th input:checkbox").live('click', function() {
                    var checkedStatus;
                    checkedStatus = this.checked;
                    $("table.tableview tbody tr td:first-child input:checkbox").each(function() {
                        return this.checked = checkedStatus;
                    });
                    return true;
                });
                

                The only way I can get the return statement inside the function is by putting it all the way like this:

                $("table.tableview tbody tr td:first-child input:checkbox").each ->
                    this.checked = checkedStatus
                true
                

                What am I doing wrong ? Thx for the help so far

                解决方案

                Just add a true as the last line of your function, and coffeescript will compile the JS to return that instead:

                $("table.tableview th input:checkbox").live 'click', -> 
                    checkedStatus = this.checked
                    $("table.tableview tbody tr td:first-child input:checkbox").each ->
                        this.checked = checkedStatus
                    true
                

                In other words, CoffeeScript always returns the result of the last line (like Ruby does)


                Edit (after the question was updated):

                Again, you can't keep CoffeeScript from returning the value of the last line in a function - part of the point of CoffeeScript is that it does exactly that.

                CoffeeScript has significant whitespace, so indentation is what says what belongs together - your example is is actually correct:

                $("table.tableview th input:checkbox").live 'click', -> 
                    checkedStatus = this.checked
                    $("table.tableview tbody tr td:first-child input:checkbox").each ->
                        this.checked = checkedStatus
                        true // cause this function (the each-iterator) to return true
                    true // causes the click handler function to return true
                

                There's no difference between this and just writing return true in a function like you would in javascript. You just use whitespace instead of {} to make code blocks.

                这篇关于CoffeeScript 总是以匿名函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:javascript中的序数 下一篇:Rails 3.1 ajax:成功处理

                相关文章

              • <tfoot id='WRxXL'></tfoot>

                  1. <small id='WRxXL'></small><noframes id='WRxXL'>

                    • <bdo id='WRxXL'></bdo><ul id='WRxXL'></ul>
                  2. <legend id='WRxXL'><style id='WRxXL'><dir id='WRxXL'><q id='WRxXL'></q></dir></style></legend>
                    <i id='WRxXL'><tr id='WRxXL'><dt id='WRxXL'><q id='WRxXL'><span id='WRxXL'><b id='WRxXL'><form id='WRxXL'><ins id='WRxXL'></ins><ul id='WRxXL'></ul><sub id='WRxXL'></sub></form><legend id='WRxXL'></legend><bdo id='WRxXL'><pre id='WRxXL'><center id='WRxXL'></center></pre></bdo></b><th id='WRxXL'></th></span></q></dt></tr></i><div id='WRxXL'><tfoot id='WRxXL'></tfoot><dl id='WRxXL'><fieldset id='WRxXL'></fieldset></dl></div>