diff --git a/docs/developer/index.md b/docs/developer/index.md
new file mode 100644
index 000000000..6396eaa21
--- /dev/null
+++ b/docs/developer/index.md
@@ -0,0 +1,6 @@
+# Developer documentation
+
+- [Patternslib overview](overview.md)
+- [The structure of a pattern](patterns.md)
+- [Configuring a pattern](patterns-configuration.md)
+- [Styleguide](styleguide.md)
diff --git a/docs/developer/patterns.rst b/docs/developer/patterns.rst
index d71b22b7d..5edf115a6 100644
--- a/docs/developer/patterns.rst
+++ b/docs/developer/patterns.rst
@@ -1,129 +1,58 @@
-Creating a new pattern
-======================
+The structure of a pattern
+==========================
-Patterns are implemented as javascript objects that are registered with the
-patterns library. Below is a minimal skeleton for a pattern.
+Patterns are implemented as JavaScript classes that are registered with the patterns registry.
+Below is a minimalistic skeleton for a pattern with explanations as inline comments.
.. code-block:: javascript
:linenos:
- define([
- 'require'
- '../registry'
- ], function(require, registry) {
- var pattern_spec = {
- name: "mypattern",
- };
-
- registry.register(pattern_spec);
- });
-
-This skeleton does several things:
-
-* lines 1-4 use `RequireJS
${example_option}, this is the ${this.name} pattern!
+ `; + } + } - inject: function($trigger, content) { - ... - } - }; + // Register Pattern class in the global pattern registry and make it usable there. + registry.register(Pattern); -The inject methods gets a number of parameters: - -* ``$trigger`` is the element that triggered the injection. -* ``content`` is an array containing the loaded content. + // Export Pattern as default export. + // You can import it as ``import AnyName from "./{{{ pattern.name }}}";`` + export default Pattern; + // Export BasePattern as named export. + // You can import it as ``import { Pattern } from "./{{{ pattern.name }}}";`` + export { Pattern }; @@ -131,8 +60,7 @@ Pattern configuration --------------------- The configuration of a pattern is generally based on three components: the -default settings, configuration set on a DOM element via a data-attribute, and, -if the jQuery API is used, via options passed in via the jQuery plugin API. +default settings, configuration set on a DOM element via a data-attribute. The init method for patterns should combine these settings. Let's update our example pattern to do this: @@ -163,43 +91,3 @@ parser instance and add our options with their default values. In the init method we use the parser to parse the ``data-mypattern`` attribute for the element. Finally we combine that with the options that might have been provided through the jQuery plugin API. - -Creating a JavaScript API -------------------------- - -Sometimes you may want to create a JavaScript API that is not tied to DOM -elements, so exposing it as a jQuery plugin does not make sense. This can -be done using the standard RequireJS mechanism by creating and returning an -API object. - -.. code-block:: javascript - :linenos: - :emphasize-lines: 13-17 - - define([ - 'require', - '../registry' - ], function(require, registry) { - var pattern_spec = { - init: function($el) { - ... - }; - }; - - registry.register(pattern_spec); - - var public_api = { - method1: function() { .... }, - method2: function() { .... } - }; - return public_api; - }); - - -You can then use the API by using require to retrieve the API object for -the pattern: - -.. code-block:: javascript - - var pattern_api = require("patterns/mypattern"); - pattern_api.method1(); diff --git a/src/core/basepattern.md b/src/core/basepattern.md index 3fdbc881d..3896f028c 100644 --- a/src/core/basepattern.md +++ b/src/core/basepattern.md @@ -16,7 +16,7 @@ Also see: https://github.com/Patternslib/pat-PATTERN_TEMPLATE import registry from "@patternslib/patternslib/src/core/registry"; export const parser = new Parser("test-pattern"); - parser.addArgument("example-option", "Stranger"); + parser.addArgument("example-option", "Hollareidulio"); class Pattern extends BasePattern { static name = "test-pattern"; @@ -33,14 +33,18 @@ Also see: https://github.com/Patternslib/pat-PATTERN_TEMPLATE // The options are automatically created, if parser is defined. const example_option = this.options.exampleOption; this.el.innerHTML = ` -hello, ${example_option}, this is pattern ${this.name} speaking.
+${example_option}, this is the ${this.name} pattern!
`; } } - // Register Pattern class in the global pattern registry + // Register Pattern class in the global pattern registry and make it usable there. registry.register(Pattern); - // Make it available + // Export Pattern as default export. + // You can import it as ``import AnyName from "./{{{ pattern.name }}}";`` export default Pattern; + // Export BasePattern as named export. + // You can import it as ``import { Pattern } from "./{{{ pattern.name }}}";`` + export { Pattern };