admin管理员组

文章数量:1357276

I have a great many loops in my coffeescript that iterate over a collection of DOM elements, and execute more jQuery. These functions tend to look like this:

$('.iterable.object').each ->
    $(@).doThis
    $(@).doThat

    ## More plicated usage
    $(@).jqueryPluginCall
        x: $(@).data('attr1')
        x: $(@).data('attr2')

    ## More plicated usage
    $(@).children('ul.animateable').each ->
        if $(@).data('animation') is "fancy"
            $(@).animate fancy: animation
        else
            $(@).animate simple: animation
        $(@).focus(
            ->
                $(@).animate some: more
            , ->
                $(@).animate even: more
        ) ## Or however you do double callbacks

I made about 3 typos entering the $(@).'s again and again, and it's being a pain.

There isn't a shortcut syntax for $(@)? It's kind of a pain to type and seems like a pretty mon syntax. It'd be cool if it operated similarly to @ as an automated function caller, like &doThis instead of &.doThis.

EDIT: I'd like to be able to define an alias right in javascript after jQuery loads that responds with the DOM element when called & and chains to methods &doThis, rather than at the top of each loop the way @bennedich suggests below.

I have a great many loops in my coffeescript that iterate over a collection of DOM elements, and execute more jQuery. These functions tend to look like this:

$('.iterable.object').each ->
    $(@).doThis
    $(@).doThat

    ## More plicated usage
    $(@).jqueryPluginCall
        x: $(@).data('attr1')
        x: $(@).data('attr2')

    ## More plicated usage
    $(@).children('ul.animateable').each ->
        if $(@).data('animation') is "fancy"
            $(@).animate fancy: animation
        else
            $(@).animate simple: animation
        $(@).focus(
            ->
                $(@).animate some: more
            , ->
                $(@).animate even: more
        ) ## Or however you do double callbacks

I made about 3 typos entering the $(@).'s again and again, and it's being a pain.

There isn't a shortcut syntax for $(@)? It's kind of a pain to type and seems like a pretty mon syntax. It'd be cool if it operated similarly to @ as an automated function caller, like &doThis instead of &.doThis.

EDIT: I'd like to be able to define an alias right in javascript after jQuery loads that responds with the DOM element when called & and chains to methods &doThis, rather than at the top of each loop the way @bennedich suggests below.

Share Improve this question edited Aug 10, 2012 at 0:47 Chris Keele asked Aug 7, 2012 at 18:47 Chris KeeleChris Keele 3,4733 gold badges32 silver badges54 bronze badges 5
  • 4 Maybe I'm just getting old, but $(@) is already pretty terse.... – J. Holmes Commented Aug 7, 2012 at 18:54
  • I'm kind of fussy. And I have a lot of these guys. (500+) – Chris Keele Commented Aug 7, 2012 at 18:56
  • Shouldn't it be $('.iterable.object').each -> ... ? – epidemian Commented Aug 7, 2012 at 19:25
  • People tend to forget that often $("foo").each(function() { $(this).bar(); }); is the same as just $("foo").bar(). Isn't it your case? – thorn0 Commented Aug 7, 2012 at 20:09
  • I'm actually executing more plicated CS within that loop. This was an example, but I'll update to demonstrate a layer of plexity. – Chris Keele Commented Aug 7, 2012 at 20:24
Add a ment  | 

1 Answer 1

Reset to default 8

How about function chaining:

$('.iterable.object').each ->
  $(@)
    .doThis()
    .doThat()

Or storing $(@) to a variable:

$('.iterable.object').each ->
  t = $(@)
  t.doThis()
  t.doThat()

Or a bination of the two. Last thing I can think of is IDE snippets, e.g. textmate will let you configure letter+TAB be replaced by $(@).

本文标签: javascriptIterating over jQuery DOM elements in coffeescript () shortcutStack Overflow