summaryrefslogtreecommitdiff
path: root/doc/development/frontend.md
diff options
context:
space:
mode:
authorZ.J. van de Weg <git@zjvandeweg.nl>2016-11-17 21:56:38 +0100
committerZ.J. van de Weg <git@zjvandeweg.nl>2016-11-17 21:56:38 +0100
commit778b5a5a04c4861c84408c944fa8dc01411cbf55 (patch)
tree04027e55d3dfdfb35c521577c8f28db09a394ee6 /doc/development/frontend.md
parent166ee0965bacc20e2ad1187321654499a9b0f825 (diff)
parentda57eb39cd2e5d8dd92b05d16f49681f1677f3e8 (diff)
downloadgitlab-ce-778b5a5a04c4861c84408c944fa8dc01411cbf55.tar.gz
Merge remote-tracking branch 'origin/master' into zj-slash-commands-mattermost
Diffstat (limited to 'doc/development/frontend.md')
-rw-r--r--doc/development/frontend.md51
1 files changed, 51 insertions, 0 deletions
diff --git a/doc/development/frontend.md b/doc/development/frontend.md
index ec8f2d6531c..9e782ab977f 100644
--- a/doc/development/frontend.md
+++ b/doc/development/frontend.md
@@ -205,6 +205,57 @@ command line.
Please note: Not all of the frontend fixtures are generated. Some are still static
files. These will not be touched by `rake teaspoon:fixtures`.
+## Design Patterns
+
+### Singletons
+
+When exactly one object is needed for a given task, prefer to define it as a
+`class` rather than as an object literal. Prefer also to explicitly restrict
+instantiation, unless flexibility is important (e.g. for testing).
+
+```
+// bad
+
+gl.MyThing = {
+ prop1: 'hello',
+ method1: () => {}
+};
+
+// good
+
+class MyThing {
+ constructor() {
+ this.prop1 = 'hello';
+ }
+ method1() {}
+}
+
+gl.MyThing = new MyThing();
+
+// best
+
+let singleton;
+
+class MyThing {
+ constructor() {
+ if (!singleton) {
+ singleton = this;
+ singleton.init();
+ }
+ return singleton;
+ }
+
+ init() {
+ this.prop1 = 'hello';
+ }
+
+ method1() {}
+}
+
+gl.MyThing = MyThing;
+
+```
+
## Supported browsers
For our currently-supported browsers, see our [requirements][requirements].