---
title: Scopes
project: jComponent V19
version: 19.194
section: guides
language: de
license: MIT
url: https://totaljs.es/docs/de/jcomponent-v19/19-194/guides/scopes/
updated: 2026-11-18T13:55:44.124Z
---

# Scopes

> Gib einem Teil der Seite einen eigenen Basis-Path, damit dasselbe Markup mit unterschiedlichen Daten arbeiten kann.

Ein Scope gibt einem Teil der Seite einen eigenen Basis-Path. Innerhalb des Scopes steht das Zeichen `?` für diesen Path. So kannst du dasselbe Markup mit anderen Daten wiederverwenden, und ein Block der Seite muss nicht wissen, wo seine Daten gespeichert sind.

## Einen Scope erstellen

Umschließe das Markup mit einem `<ui-plugin>`-Element und beginne die inneren Paths mit `?`.

```html
<ui-plugin path="billing">
	<ui-bind path="?.name" config="text"></ui-bind>
	<ui-component name="field" path="?.vat"></ui-component>
</ui-plugin>

<ui-plugin path="shipping">
	<ui-bind path="?.name" config="text"></ui-bind>
	<ui-component name="field" path="?.vat"></ui-component>
</ui-plugin>

<script>
	COMPONENT('field', function(self) {
		self.setter = function(value) {
			self.text(self.path + ' = ' + value);
		};
	});

	SET('billing', { name: 'Ana', vat: 'A1' });
	SET('shipping', { name: 'Ben', vat: 'B2' });
</script>
```

Der erste Block zeigt `Ana billing.vat = A1` und der zweite `Ben shipping.vat = B2`. Außerhalb eines Scopes ist `?` ein Fehler: Die Browserkonsole zeigt die Warnung `Missing <ui-plugin>`, und die Bindung wird nicht erstellt.

## Verschachtelte Scopes

Ein Scope innerhalb eines anderen Scopes erweitert den Path seines übergeordneten Scopes. Ein `!` vor dem Namen macht den Scope isoliert, sodass er den übergeordneten Scope ignoriert. `?1` verweist auf den übergeordneten Scope, `?2` auf dessen übergeordneten Scope und so weiter.

```html
<ui-plugin id="order" path="order">
	<ui-plugin id="address" path="address">
		<ui-bind id="city" path="?.city" config="text"></ui-bind>
		<ui-bind id="ref" path="?1.ref" config="text"></ui-bind>
	</ui-plugin>
	<ui-plugin id="shared" path="!shared">
		<ui-bind id="note" path="?.note" config="text"></ui-bind>
	</ui-plugin>
</ui-plugin>

<script>
	SET('order', { ref: 'R-1', address: { city: 'Rome' } });
	SET('shared.note', 'Isolated');
</script>
```

| Element | Path des Scopes |
| --- | --- |
| `<ui-plugin path="order">` | `order` |
| `<ui-plugin path="address">` darin | `order.address` |
| `<ui-plugin path="!shared">` darin | `shared` |

Im Beispiel liest `?.city` den Wert `order.address.city` und `?1.ref` den Wert `order.ref`.

## Einen Scope konfigurieren

| Attribut oder Einstellung | Wirkung |
| --- | --- |
| `path="?"` | Erzeugt einen eindeutigen Namen für den Scope. |
| `default="{ ... }"` | Speichert diesen Wert beim Erstellen des Scopes unter dessen Path. |
| `config="init:name"` | Ruft beim Erstellen des Scopes die globale Funktion `name(path, element)` auf. |
| `config="aclass:1"` | Fügt dem Element den Path des Scopes als CSS-Klasse hinzu. |
| `config="class:name"` | Schaltet kurz nach dem Erstellen eine CSS-Klasse am Element um. |

```html
<ui-plugin path="?">
	<ui-bind id="a" path="?.title" config="text"></ui-bind>
</ui-plugin>

<ui-plugin path="login" default="{ user: 'guest', remember: false }" config="init:loginready;aclass:1;class:ready">
	<ui-bind id="b" path="?.user" config="text"></ui-bind>
</ui-plugin>

<script>
	function loginready(path, element) {
		console.log('init', path, element.prop('tagName'));
	}
</script>
```

Der zweite Scope speichert `{"user":"guest","remember":false}` in `login`, und sein Element erhält die Klassen `login` und `ready`.

## Ein vorhandenes Element als Scope verwenden

Die Attribute `scope`, `plugin` und `data-scope` machen aus jedem Element einen Scope.

```html
<section scope="panel">
	<ui-bind id="a" path="?.title" config="text"></ui-bind>
</section>

<div plugin="sidebar">
	<ui-bind id="b" path="?.title" config="text"></ui-bind>
</div>

<div data-scope="footer">
	<ui-bind id="c" path="?.title" config="text"></ui-bind>
</div>

<script>
	SET('panel.title', 'Panel');
	SET('sidebar.title', 'Sidebar');
	SET('footer.title', 'Footer');
</script>
```

## Im Code mit einem Scope arbeiten

`$(element).scope()` gibt den Scope zurück, zu dem ein Element gehört. Paths, die du seinen Methoden übergibst, sind relativ zum Scope.

```html
<ui-plugin id="panel" path="form">
	<ui-component name="field" path="?.name"></ui-component>
	<ui-component name="field" path="?.city"></ui-component>
</ui-plugin>

<script>
	COMPONENT('field', function(self) {
		self.setter = function(value) {
			self.text(value);
		};
	});

	ON('ready', function() {

		var scope = $('#panel').scope();

		console.log(scope.path);                // form
		console.log(scope.makepath('?.name'));  // form.name

		scope.watch('name', (path, value) => console.log('watch', path, value));

		scope.set('name', 'Ana');               // watch form.name Ana
		scope.set('city', 'Rome');
		console.log(scope.get('name'));         // Ana
		console.log(scope.get());               // {"name":"Ana","city":"Rome"}

		scope.push('tags', 'new');
		console.log(form.tags);                 // ["new"]

		console.log(scope.FIND('field').path);  // form.name
	});
</script>
```

| Member | Beschreibung |
| --- | --- |
| `path` | Der vollständige Path des Scopes. |
| `element` | Das Element des Scopes als jQuery-Objekt. |
| `parent` | Der übergeordnete Scope. |
| `makepath(path)` | Ersetzt `?` durch den Path des Scopes. |
| `get([path])` | Liest einen Wert. Ohne Path gibt die Methode das gesamte Objekt zurück. |
| `set(path, value)` | Schreibt einen Wert. |
| `push(path, value)` | Fügt einem Array einen Wert hinzu. |
| `update(path)` | Benachrichtigt alles, was von einem Path abhängt. |
| `watch(path, fn, [init])` | Überwacht einen Path. |
| `FIND(selector)` | Sucht eine Komponente innerhalb des Scopes. |
| `SETTER(selector, method, ...)` | Ruft eine Methode der Komponenten innerhalb des Scopes auf. |

Die Referenz zu [Scope](../../reference/scope/) führt alle Member auf.

## Ein Plugin anbinden

Ein [Plugin](../plugins/) mit demselben Namen wie der Scope enthält die Logik dieses Seitenteils. Innerhalb des Scopes verweist `?/name` auf eine Funktion des Plugins, und im Code des Plugins steht `?` für seinen Path.

```html
<ui-plugin path="contact">
	<button id="save" data-bind="?.name__click:?/save">Save</button>
	<ui-bind id="label" path="?.name" config="text:?/format"></ui-bind>
</ui-plugin>

<script>
	PLUGIN('contact', function(exports) {

		exports.format = function(value) {
			return (value || '').toUpperCase();
		};

		exports.save = function(el, e, value, path) {
			console.log('saving', value, 'from', path);
			SET('?.saved', true);
		};
	});

	SET('contact.name', 'Ana');
</script>
```

Das Label zeigt `ANA`. Ein Klick auf die Schaltfläche gibt `saving Ana from contact.name` aus und speichert `true` in `contact.saved`.
