Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facilitate jQuery-like chaining #944

Closed
dbrans opened this issue Dec 17, 2010 · 5 comments
Closed

Facilitate jQuery-like chaining #944

dbrans opened this issue Dec 17, 2010 · 5 comments

Comments

@dbrans
Copy link

dbrans commented Dec 17, 2010

Since jQuery-like chaining is very popular in javascript would it make sense to support (and improve) it in coffeescript?

javascript:
$('#foo')
.attr('a', 'boo')
.append(c, d, e)
.show()

The equivalent cs is identical.

However, It would be great if we could instead write (note there is no indentation):
$ '#foo'
.attr 'a', 'boo'
.append c, d, e
.show()
i.e., if a line starts with a dot and is not indented coffeescript understands that chaining is implied with the result of the entire previous line. (An indented line would still result in the current behavior)

@satyr
Copy link
Collaborator

satyr commented Dec 17, 2010

Yes.

@timmfin
Copy link

timmfin commented Dec 17, 2010

Personally, I prefer indentation when chaining. I would probably write that code as:

$('#foo').attr('a', 'boo')
         .append(c, d, e)
         .show()

@satyr
Copy link
Collaborator

satyr commented Dec 17, 2010

Indentation doesn't matter in this case actually. A line leading with a dot is considered continued and thus doesn't terminate an expression or create a block.

What's missing here is a rule to make such dots close implicit calls.

@TrevorBurnham
Copy link
Collaborator

This has come up a few times before, but without a satisfactory consensus. There's likely to be an overhaul of how indentation and function arguments are handled at some point, though not for 1.0. Someone needs to make a concrete proposal on how every possible case should be handled.

For instance, if we wanted to support jQuery-style chaining without parentheses, we'd need to support things like

$this
  .append c, d, $that
    .show()

where the indentation of the last line means that show() is called on $that; without the indentation, show() would be called on $this. Currently, show() is called on $that with or without the extra indentation, which is why you need parentheses on the append line to clarify that you want to keep chaining with $this.

@jashkenas
Copy link
Owner

This has come up before, and I'm still of the opinion that it's an abuse of indentation. Leading dot accessors should attach to the final value of the previous line, and not cause the previous line to be implicitly wrapped in parentheses.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants