1
- Creating a new pattern
2
- ======================
1
+ The structure of a pattern
2
+ ==========================
3
3
4
- Patterns are implemented as JavaScript classes that are registered with the patterns library .
5
- Below is a minimal skeleton for a pattern.
4
+ Patterns are implemented as JavaScript classes that are registered with the patterns registry .
5
+ Below is a minimalistic skeleton for a pattern with explanations as inline comments .
6
6
7
7
.. code-block :: javascript
8
8
: linenos:
9
9
10
10
import { BasePattern } from " @patternslib/patternslib/src/core/basepattern" ;
11
+ import Parser from " @patternslib/patternslib/src/core/parser" ;
11
12
import registry from " @patternslib/patternslib/src/core/registry" ;
12
13
14
+ export const parser = new Parser (" test-pattern" );
15
+ // Define an argument with a default value. You can configure the value via
16
+ // data-attributes.
17
+ parser .addArgument (" example-option" , " Hollareidulio" );
18
+
13
19
class Pattern extends BasePattern {
20
+
21
+ // Define a name for the pattern which is used as key in the pattern
22
+ // registry.
14
23
static name = " test-pattern" ;
24
+
25
+ // Define a CSS selector. The pattern will be initialized on elements
26
+ // matching this selector.
15
27
static trigger = " .pat-test-pattern" ;
16
28
29
+ // The parser instance from above.
30
+ static parser = parser;
31
+
17
32
init () {
33
+ import (" ./test-pattern.scss" );
34
+
35
+ // Try to avoid jQuery, but here is how to import it, asynchronously.
36
+ // eslint-disable-next-line no-unused-vars
37
+ const $ = (await import (" jquery" )).default ;
38
+
39
+ // The options are automatically created, if parser is defined.
40
+ const example_option = this .options .exampleOption ;
41
+ this .el .innerHTML = `
42
+ <p >${ example_option} , this is the ${ this .name } pattern!</p >
43
+ ` ;
18
44
}
19
45
}
20
46
@@ -29,55 +55,12 @@ Below is a minimal skeleton for a pattern.
29
55
export { Pattern };
30
56
31
57
32
- This skeleton does several things:
33
-
34
- * lines 1-4 use `RequireJS <http://requirejs.org/ >`_ to load the patterns
35
- registry.
36
- * lines 5-7 create an object which defines this pattern's specifications.
37
- * line 9 registers the pattern.
38
-
39
-
40
- Markup patterns
41
- ---------------
42
-
43
- Most patterns deal with markup: they are activated for content that matches
44
- a specific CSS selector. This is handled by adding two items to the
45
- pattern specification: a ``trigger `` and an ``init `` function.
46
-
47
- .. code-block :: javascript
48
- : linenos:
49
-
50
- var pattern_spec = {
51
- name: " mypattern" ,
52
- trigger: " .tooltip, [data-tooltip]" ,
53
-
54
- init : function ($el ) {
55
- ...
56
- },
57
-
58
- destroy : function ($el ) {
59
- ...
60
- }
61
- };
62
-
63
- The trigger specified on line 3 is a CSS selector which tells the pattern
64
- framework which elements this pattern is interested in. If new items are
65
- discovered in the DOM that match this pattern, the ``init `` function will be
66
- called with a jQuery wrapper around the element.
67
-
68
- While not required patterns are encouraged to include a ``destroy `` function
69
- that undos the pattern initialisation. After calling ``destroy `` it should be
70
- possible to call ``init `` again to reactivate the pattern.
71
-
72
- Methods must always return ``this `` to facilitate their use as jQuery widgets.
73
-
74
58
75
59
Pattern configuration
76
60
---------------------
77
61
78
62
The configuration of a pattern is generally based on three components: the
79
- default settings, configuration set on a DOM element via a data-attribute, and,
80
- if the jQuery API is used, via options passed in via the jQuery plugin API.
63
+ default settings, configuration set on a DOM element via a data-attribute.
81
64
The init method for patterns should combine these settings. Let's update our
82
65
example pattern to do this:
83
66
0 commit comments