# Zoskup stav a logiku do pluginu jComponent

> Deklaruj plugin cez PLUGIN() a pripoj ho k časti stránky cez ui-plugin, pričom ? je jeho vlastná cesta.

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: sk
Content-SHA256: 688a07a5a3e322a341a3b06be1d971f34edab1c53ed33fa11961850edde19b47
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: jcomponent20@3fedd009a277f50fddc608b97afbec553b67c51b
Review: not-recorded

Plugin je kontrolér jednej sekcie stránky. `PLUGIN(name, declaration)` ho zaregistruje a `<ui-plugin path="name">` ho pripojí k elementu. Vnútri sekcie znamená `?` v ceste "cesta pluginu", takže značkovanie nezávisí od toho, kde plugin v stave žije.

Deklarácia dostane `self` (inštanciu pluginu) a `config`. `self.get()` a `self.set()` pracujú relatívne k ceste pluginu a `self.element` je jQuery element `<ui-plugin>`. Implementuj `self.make`, aby sa kód spustil, keď je plugin pripravený. Pre udalosti DOM použi `self.element`: `self.on()` je pre udalosti jComponent, nie pre kliknutia.

```html public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>

	<ui-plugin path="cart">
		<p>Items: <ui-bind path="?.items" config="text:value || 0"></ui-bind></p>
		<button type="button" id="add">Add item</button>
	</ui-plugin>

	<script>

		PLUGIN('cart', function(self) {

			self.add = function() {
				self.set('items', (self.get('items') || 0) + 1);
			};

			// make() runs when the plugin is ready: initialize the state and the events here
			self.make = function() {
				self.set('items', 0);
				self.element.on('click', '#add', self.add);
			};

		});

	</script>

</body>
</html>
```
