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.
-
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
1 Answer
Reset to default 8How 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
版权声明:本文标题:javascript - Iterating over jQuery DOM elements in coffeescript: $(@). shortcut - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744075088a2586595.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论