Iterate over jQuery DOM elements in coffeescript file: $ (@). abbreviated

I have many loops in my coffee pot that iterate over a set of DOM elements and do more jQuery. These features tend to look like this:

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

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

    ## More complicated 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, typing $(@).over and over again, and it becomes a pain.

Shortcut syntax for $(@)missing? This is a kind of pain to type in and seems to be a fairly common syntax. It would be great if it worked similarly @as an automated caller, for example &doThisinstead &.doThis.

EDIT: javascript jQuery, DOM & &doThis, , @bennedich .

+5
1

:

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

$(@) :

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

. , , IDE, . textmate + TAB $(@).

+8

All Articles