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

# Scopes

> Give a part of the page its own base path, so the same markup can work with different data.

A scope gives a part of the page its own base path. Inside it, the `?` character stands for that path. The same markup can then be reused with different data, and a block of the page does not need to know where its data is stored.

## Create a scope

Wrap the markup in a `<ui-plugin>` element and start the inner paths with `?`.

```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>
```

The first block shows `Ana billing.vat = A1` and the second `Ben shipping.vat = B2`. Using `?` outside a scope is an error: the browser console shows the warning `Missing <ui-plugin>` and the binding is not created.

## Nested scopes

A scope inside another scope extends the path of its parent. A `!` in front of the name makes the scope isolated, so it ignores the parent. `?1` refers to the parent scope, `?2` to the parent of the parent, and so on.

```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 of the scope |
| --- | --- |
| `<ui-plugin path="order">` | `order` |
| `<ui-plugin path="address">` inside it | `order.address` |
| `<ui-plugin path="!shared">` inside it | `shared` |

In the example `?.city` reads `order.address.city`, and `?1.ref` reads `order.ref`.

## Configure a scope

| Attribute or setting | Effect |
| --- | --- |
| `path="?"` | Generates a unique name for the scope. |
| `default="{ ... }"` | Stores this value at the path of the scope when the scope is created. |
| `config="init:name"` | Calls the global function `name(path, element)` when the scope is created. |
| `config="aclass:1"` | Adds the path of the scope as a CSS class on the element. |
| `config="class:name"` | Toggles a CSS class on the element shortly after it is created. |

```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>
```

The second scope stores `{"user":"guest","remember":false}` in `login`, and its element receives the classes `login` and `ready`.

## Use an existing element as a scope

The attributes `scope`, `plugin` and `data-scope` turn any element into a 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>
```

## Work with a scope in code

`$(element).scope()` returns the scope that an element belongs to. Paths given to its methods are relative to the 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 | Description |
| --- | --- |
| `path` | The full path of the scope. |
| `element` | The element of the scope, as a jQuery object. |
| `parent` | The parent scope. |
| `makepath(path)` | Replaces `?` with the path of the scope. |
| `get([path])` | Reads a value. Without a path it returns the whole object. |
| `set(path, value)` | Writes a value. |
| `push(path, value)` | Adds a value to an array. |
| `update(path)` | Notifies the dependents of a path. |
| `watch(path, fn, [init])` | Watches a path. |
| `FIND(selector)` | Finds a component inside the scope. |
| `SETTER(selector, method, ...)` | Calls a method of the components inside the scope. |

The [Scope](../../reference/scope/) reference lists all the members.

## Connect a plugin

A [plugin](../plugins/) with the same name as the scope holds the logic of that part of the page. Inside the scope, `?/name` refers to a function of the plugin, and `?` in the code of the plugin stands for its 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>
```

The label shows `ANA`. Clicking the button prints `saving Ana from contact.name` and stores `true` in `contact.saved`.
