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
const MyThing = {
prop1: 'hello',
method1: () => {}
};
export default MyThing;
// good
class MyThing {
constructor() {
this.prop1 = 'hello';
}
method1() {}
}
export default new MyThing();
// best
export default class MyThing {
constructor() {
if (!MyThing.prototype.singleton) {
this.init();
MyThing.prototype.singleton = this;
}
return MyThing.prototype.singleton;
}
init() {
this.prop1 = 'hello';
}
method1() {}
}
Manipulating the DOM in a JS Class
When writing a class that needs to manipulate the DOM guarantee a container option is provided. This is useful when we need that class to be instantiated more than once in the same page.
Bad:
class Foo {
constructor() {
document.querySelector('.bar');
}
}
new Foo();
Good:
class Foo {
constructor(opts) {
document.querySelector(`${opts.container} .bar`);
}
}
new Foo({ container: '.my-element' });
You can find an example of the above in this class;
Help and feedback
If there's something you don't like about this feature
To propose functionality that GitLab does not yet offer
To further help GitLab in shaping new features
If you didn't find what you were looking for
If you want help with something very specific to your use case, and can use some community support
POST ON GITLAB FORUM
If you have problems setting up or using this feature (depending on your GitLab subscription)
REQUEST SUPPORT
To view all GitLab tiers and features or to upgrade
If you want to try all features available in GitLab.com
If you want to try all features available in GitLab self-managed
If you spot an error or a need for improvement and would like to fix it yourself in a merge request
EDIT THIS PAGE
If you would like to suggest an improvement to this doc