-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
## Javascript restructure | ||
|
||
### Motivation | ||
|
||
A style guide is provided for Javascript in Gov.UK sites at https://github.com/alphagov/govuk-frontend/blob/main/docs/contributing/coding-standards/js.md | ||
|
||
Currently the Javascript modules in CCCD do not follow this guide and instead modules are defined in two different ways; | ||
|
||
Executing a function expression to return an object; | ||
|
||
```javascript | ||
// app/webpack/javascripts/modules/Helpers.API.Core.js | ||
|
||
(function (exports, $) { | ||
const Module = exports.Helpers.API || {} | ||
|
||
... | ||
|
||
Module._CORE = { | ||
query | ||
} | ||
|
||
exports.Helpers.API = Module | ||
}(moj, jQuery)) | ||
``` | ||
|
||
Adding to the `moj` module so that it is initialized together with all modules by `moj.init()` in `app/webpack/javascripts/application.js`; | ||
|
||
```javascript | ||
// app/webpack/javascripts/modules/Helpers.DataTables.js | ||
|
||
moj.Helpers.DataTables = { | ||
init: function (options, el) { | ||
... | ||
}, | ||
|
||
... | ||
} | ||
``` | ||
|
||
The Gov.UK style guide recommends using the class design pattern to structure the code; | ||
|
||
```javascript | ||
class Example { | ||
// Code goes here | ||
} | ||
``` | ||
|
||
The primary benefits of refactoring our Javascript is to have a single, consistent style so that future developers can better understand the codebase. | ||
Specifically using the recommended Gov.UK style will further facilitate new developers who have worked on other Gov.UK projects. | ||
|
||
This is a *work in progress* so javascript modules may be written in any of the above formats but the ideal is the class design pattern. |