This patch contains all kinds of minor code style fixes and cruft removals that don't warrant creating a patch of their own.
| Comment | File | Size | Author |
|---|---|---|---|
| #30 | drupal.parseint.patch | 3.92 KB | sun |
| #23 | js-function.patch | 88.68 KB | robloach |
| #22 | js-function-1.patch | 83.84 KB | kkaefer |
| #15 | 444402.patch | 58.93 KB | robloach |
| #12 | js-cruft-3.patch | 55.39 KB | kkaefer |
Comments
Comment #1
kkaefer commentedComment #2
caktux commentedfollow up of http://drupal.org/node/444344 ... :)
Comment #3
kkaefer commentedThis is not a follow-up; the other patch slipped into this patch as well, unfortunately.
Here is an improved version of this patch with more code-style fixes:
function() {...}instead offunction () {...}{ key: 'value' }instead of{key: 'value'}'string' + variableinstead of'string'+ variabletypeof variableinstead oftypeof(variable)'string'instead of"string"func(foo, bar)instead offunc( foo, bar )$('<div></div>')instead ofdocument.createElement('div')object.memberinstead ofobject['member']$.isFunction(fn)instead oftypeof fn == 'function'[]instead ofnew Array()and{}instead ofnew Object()variableNameinstead of$variableNameand various other minor fixes.
Comment #4
kkaefer commentedWhen reviewing, please clear your cache as Drupal's .htaccess caches JavaScript quite aggressively.
Comment #5
stewsnoozeThis seems good. It's simple formatting changes e.t.c as you say!
Let's wait for the bot to come along then we can get it in.
Comment #6
kkaefer commentedThe bot's review is completely meaningless because it doesn't test JavaScript.
Comment #7
robloachThis looks most appropriate and these standards should be documented in the Drupal coding standards.
This looks RTBC to me, except for the use of
parseInt(___, 10). Is there any reason why we're explicitly telling parseInt to use the decimal numeric system? It defaults to decimal. Here it is without the 10, because it defaults to 10.......Comment #8
dmitrig01 commentedRob's patch is good - I read through it and there are only code style changes, no actual code changes.
Comment #9
webchickAh, great patch! :)
I actually find the old code much more legible than the new code. But is this considered a standard jQuery way to do things? If so, I'm fine with letting this slide, but that first one is definitely a 'wtf' and needs a comment explaining what it's doing. But if there isn't a 'standard' that says "write these things this way" my preference is the more legible one even though it's less 'clever.'
That looks like a mistake. Or is it to imply that .append is being appended to .after? If so, can we add a comment so the next 'helpful' person doesn't 'fix' this?
Let's get a comment above that sucker too. Wth? :)
That's quite different. Could we have confirmation from someone that batch API continues to work with this patch?
Another "wha-huh?" from me here. Again, would someone with general knowledge in JS/jQuery understand what this is, or is this something only crazy JS wizards like kkaefer understand? :) (these lines do have comments, in this case, but this construct is still very weird.) Again, if there isn't a 'standard' that says "write these things this way" my preference is the more legible one even though it's less 'clever.'
Those were the only ones that jumped out at me in glancing through.
Comment #10
webchickComment #11
kkaefer commentedYes, pretty much every jQuery plugin uses
$('<div></div>')to create elements.First of all, this change doesn’t alter the code, just the indentation. What’s happening here is that
.append()is called on$('<div class="fieldset-wrapper"></div>'), and not concatenated to.after().There was a line like
var uri = settings.batch.uri;which I removed in favor of this line.Yes, using
{}and[]instead ofnew Object()andnew Array()is standard practice and used everywhere else throughout our JavaScript code and pretty much everyone else’s JavaScript code (look at line 1 in drupal.js, for example). JSON uses{}and[]too.Comment #12
kkaefer commentedChanged this line:
to the more readable:
I didn’t add a code comment since that comment would only paraphrase what is already expressed in code pretty nicely (and understandably, imho).
Comment #13
kkaefer commentedComment #14
caktux commentedGotta say, $(this.element).before(error).hide() is plain beauty :p No errors on all browsers, everything seems to work normally too.
Comment #15
robloachThis clean up really helps the JavaScript. I've documented the typeof coding standard. We should go through the other changes made and document them in the JavaScript coding standards.
Webchick is kind of right in saying that the following looks kind of weird unless you look at it closely:
Maybe this?
There's the patch with that change. Which ever method is chosen, I believe this is RTBC.
Comment #16
webchick@RobLoach: Cool, yeah that's a bit more legible.
@kkaefer: Thanks for answering my questions and assuaging my worries/doubts. :)
Committed to HEAD. :) Thanks!
And at approximately 3:18pm Eastern on April 26th, the collective anguished moans of every JS patch author in the entire core queue could be heard around the world... Sorry, folks... but we discussed in #drupal-dev and figured there's another few hours left in the sprint to do clean-up work so sooner would be better than later. And hey, free chance to bump your patch in the queue and get some eyeballs on it! ;)
Also, let's get those coding standards documented so we never have to commit a patch like this again. ;)
Comment #17
c960657 commentedThis broke auto-completion. If I click on an auto-completion entry, e.g. an article tag, the contents of the text field is set to "undefined".
AFAICT the problem is this (though I haven't fully investigated the problem):
I don't think setting an
autocompleteValueproperty on the object is equivalent to setting anautocompleteValueattribute.Comment #18
caktux commentedwe'll have to document that when we want to set an element's attribute, we use jquery's .attr() :)
Comment #19
webchicktha_sun and stella pointed out to me that we do have JS coding standards, unbeknownst to me until just now. (I knew that there were people working on them, but I hadn't realized they'd gone through community review in the coding standards group, etc.). These standards are at http://drupal.org/node/172169.
This patch deviates from those in one important way: the space between the word "function" and the parentheses in anonymous functions. This is important because these are *not* functions, they are variables that contain a function.
So in addition to fixing autocomplete, let's back out that part of the changes. Should be a simple regex.
Comment #20
stella commentedHaving discussed this with tha_sun on IRC, we also think that we should be using
.lengthrather than.size(). .length is faster because it's a simple property that jQuery always sets, whereas .size() adds a function call.In addition, there is no current standard on the spacing style for
.attr({....}). The patch changes these lines to be like:Note the lack of a space between the parentheses and the curly brace, but the addition of a space between the curly braces and the attributes. We think the space between the { and the attributes should also be removed.
Cheers,
Stella
Comment #21
kkaefer commentedYeah, I didn’t see the coding standards for
function (). We had both types in the code and I standardized them to one, but I’m perfectly fine with the other one.However, I think that
is a lot easier on the eyes than
Comment #22
kkaefer commentedThis patch fixes the autocomplete problem by using jQuery’s
$.data()facility, which is the way how data should be associated with a DOM node in jQuery.It also changes
function()tofunction ()to be in line with the coding standards document. However, it looks like most function didn’t adhere tofunction ()even before this cruft-removing patch went in. However, I think it’s a good idea to usefunction ()since jQuery uses it and Crockford suggests that style in http://javascript.crockford.com/code.html as well.I’d also suggest adding to the coding standards, that
{ }after a function (a code block) and{}as an object definition are different things and that the first one should have a space but the second one should be empty.Note for TextMate users: the default tab trigger
f⇥producesfunction() {}instead offunction () {}.Comment #23
robloachWe're making changes in farbtastic.js here. Since it's a "third-party" plugin, those changes should be made in the Farbtastic project instead. Here's the patch without the Farbtastic change.
Tested the auto complete and it works now. RTBC.
Comment #24
webchickI still see references to size() in there; let's get those switched to .length.
I defer to stella/sun/other JS folks on the spacing question. I do agree with kkaefer that it seems more legible with the space on either side, but it's also not something we do anywhere else in core. Is there an "official" jQuery coding style reference we can draw from to settle this?
Comment #25
webchickWell, on the other hand, sun pointed out in IRC that we do currently have a critical bug here that needs fixing, and the size()->length and spacing questions are fairly minor things that could come after.
So I committed #23 to HEAD. Still leaving as needs work for #24.
Comment #26
int commenteddeleted
Comment #27
kkaefer commentedWe don't really want to follow jQuery coding styles. They are largely different from what we have now.
Comment #28
rfayIt's not at all clear to me from browsing this what the "Needs Documentation" tag was added for. Can any of you participants elaborate? Or do the documentation?
Thanks!
Comment #29
jhodgdonI'm removing the Needs Doc tag, since it appears the standards are documented, based on the above discussion.
Comment #30
sunAwesome patch!
Quickly scanned the previous follow-ups as well as the committed patch -- parseInt() always needs to specify the second parameter "10" when assuming decimals. It is not the default. The default depends on the passed in value (first argument).
farbtastic.js is not affected, because it prefixes the passed in value with "0x".
Comment #31
robloachWhitespace changes arn't really needed in this patch. ParseInt is a good one. RTBCings.
Comment #32
dries commentedWhy do we need to specify the 10? If the string begins with anything other than 0x or 0, the radix is automatically set to 10.
Comment #33
sunAlthough one can assume that in cases like this, offsetHeight is provided by the DOM, it is still commonly known as JavaScript best practice -- the script assumes a decimal value to work with, so the rowHeight can be used as decimal value for something else. Therefore, passing 10 means extra clarity.
Here, even the comment mentions that it assumes something and we try to perform calculations on an assumed decimal value. :)
This review is powered by Dreditor.
Comment #34
dries commentedFair enough. Committed to CVS HEAD. Thanks.