ForgejComponent
sk

jComponent

Zoskup stav a logiku do pluginu jComponent

jComponent 20 · jcomponent@20 jcomponentfrontendplugins Spustené s jComponent 20

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.

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>
Vyskúšajte to vo svojom prehliadači

Upravte príklad a spustite ho. Beží v izolovanom ráme vo vašom prehliadači; na náš server sa nič neposiela.

Náhľad