Skip to content

Commit

Permalink
[#540] Tweaks to the JS coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmartin committed Apr 23, 2013
1 parent 9cf791e commit 1666a04
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions doc/javascript-coding-standards.rst
Expand Up @@ -8,14 +8,14 @@ Formatting
.. _idiomatic.js: https://github.com/rwldrn/idiomatic.js/
.. _Douglas Crockford's: http://javascript.crockford.com/code.html

All JavaScript documents must use **two spaces** for indentation and files
should have no trailing whitespace. This is contrary to the `OKFN Coding
Standards`_ but matches what's in use in the current code base.
All JavaScript documents must use **two spaces** for indentation. This is
contrary to the `OKFN Coding Standards`_ but matches what's in use in the
current code base.

Coding style must follow the `idiomatic.js`_ style but with the following
exceptions.

.. note:: Idiomatic is heavily based upon `Douglas Crockford's`_ style
.. Note:: Idiomatic is heavily based upon `Douglas Crockford's`_ style
guide which is recommended by the `OKFN Coding Standards`_.

White Space
Expand Down Expand Up @@ -67,19 +67,19 @@ One ``var`` statement must be used per variable assignment. These must be
declared at the top of the function in which they are being used. ::

// GOOD:
var good = "string";
var alsoGood = "another;
var good = 'string';
var alsoGood = 'another';

// GOOD:
var good = "string";
var good = 'string';
var okay = [
"hmm", "a bit", "better"
'hmm', 'a bit', 'better'
];

// BAD:
var good = "string",
var good = 'string',
iffy = [
"hmm", "not", "great"
'hmm', 'not', 'great'
];

Declare variables at the top of the function in which they are first used. This
Expand Down Expand Up @@ -107,7 +107,7 @@ statement. ::

for (index = 0, length = names.length; index < length; index += 1) {
name = names[index];
names.push(names[index].toLowerCase());
names.push(name.toLowerCase());
}

sorted = names.sort();
Expand Down Expand Up @@ -288,10 +288,10 @@ Forms
`````

All forms should work without JavaScript enabled. This means that they must
submit ``application/x-www-form-urlencoded`` data to the server and receive an appropriate
response. The server should check for the ``X-Requested-With: XMLHTTPRequest``
header to determine if the request is an ajax one. If so it can return an
appropriate format, otherwise it should issue a 303 redirect.
submit ``application/x-www-form-urlencoded`` data to the server and receive an
appropriate response. The server should check for the ``X-Requested-With:
XMLHTTPRequest`` header to determine if the request is an ajax one. If so it
can return an appropriate format, otherwise it should issue a 303 redirect.

The one exception to this rule is if a form or button is injected with
JavaScript after the page has loaded. It's then not part of the HTML document
Expand All @@ -300,6 +300,12 @@ and can submit any data format it pleases.
Ajax
````

.. Note::
Calls to the CKAN API from JavaScript should be done through the
`CKAN client`_.

.. _CKAN client: ./frontend-development.html#client

Ajax requests can be used to improve the experience of submitting forms and
other actions that require server interactions. Nearly all requests will
go through the following states.
Expand All @@ -319,7 +325,7 @@ Here's a possible example for submitting a search form using jQuery. ::

jQuery('#search-form').submit(function (event) {
var form = $(this);
var button = form.find('[type=submit]');
var button = $('[type=submit]', form);

// Prevent the browser submitting the form.
event.preventDefault();
Expand Down

0 comments on commit 1666a04

Please sign in to comment.