简体   繁体   English

Coffeescript jQuery每个循环

[英]Coffeescript jQuery each loop

I'm relatively new to Coffeescript and am running into some issues that I think are to do with skinny arrow vs fat arrow / _this vs this vs $(this). 我对Coffeescript相对较新,并且我遇到了一些问题,我认为这些问题与瘦箭头和胖箭头/ _this vs this vs vs(this)有关。

I have the following class and constructor: 我有以下类和构造函数:

class @scenarioEditPage
  constructor: ->
    @getParentAttributes()

and @getParentAttributes() is 和@getParentAttributes()是

getParentAttributes: ->
    @properties = {}
    $("#form_configurable").find('input[type=hidden]').each (index, element) =>
      @properties[$(element).attr('id')] = element.val()

Essentially what I'm trying to do is loop through all hidden fields in a form and assign their value to a key/value pair in the properties hash that can be used later. 基本上我要做的是循环遍历表单中的所有隐藏字段,并将其值分配给属性哈希中的键/值对,以后可以使用。 Example: hidden input field with id=some_id and value=some_value becomes properties[some_id] = some_value . 示例:隐藏的输入字段,其id=some_idvalue=some_value变为properties[some_id] = some_value

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you! 谢谢!

First of all, I wouldn't use class @scenariosEditPage unless you really have to. 首先,我不会使用class @scenariosEditPage除非你真的需要。 class ScenariosEditPage would be better. class ScenariosEditPage会更好。

Try something like this: http://jsfiddle.net/y6bgrfn2/ 尝试这样的事情: http//jsfiddle.net/y6bgrfn2/

    class Test
        constructor: () ->
            @data = {}
            @loadData()

        loadData: ->
            $inputs = $("#container input").each (index, elem) =>
                $elem = $ elem
                @data[$elem.attr('id')] = $elem.val()

        getData: -> @data

    $(document).ready () ->
        test = new Test()
        console.log test.getData()

jQuery's each method changes the context of the inside code, so basically you have to use double arrow to preserve the context or simple arrow with with extra variable, for example ctx = @ , check this: jQuery的each方法都会改变内部代码的上下文,所以基本上你必须使用双箭头来保存上下文或带有额外变量的简单箭头,例如ctx = @ ,请检查:

    # We have some method @doAnything, so :
    @doAnything
    # isnt undefined...

    # Now:
    # this works
    $('selector...').each (index, elem) =>
        # @ == this == caller's context
        @doAnything $(elem)

    # or this works fine
    ctx = @
    $('selector...').each () ->
        # this == DOM Element
        # ctx == caller's context
        # ! ctx isnt @ !
        ctx.doAnything $(this)

    # this doesnt work
    $('selector...').each () =>
        # @ == DOM Element, so @doAnything is undefined
        @doAnything $(this)

And you have small bug in your code: 你的代码中有一个小错误:

    @properties[$(element).attr('id')] = element.val()

element.val() is undefined , fix it like this: element.val() undefined ,修复如下:

    @properties[$(element).attr('id')] = $(element).val()

or better: 或更好:

    $element = $ element
    @properties[$element.attr('id')] = $element.val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM