Spanish Total.js
English
Documentation For AI agents
Contents

Scope

The properties and methods of a scope object.

.md GitHub
On this page

A scope object represents a ui-plugin element in the page. Paths used through the object are relative to the path of the scope. Get the object with $(element).scope(). Scopes explains the concept.

API

property

scope.path

The absolute path of the scope.
Reading path
var scope = $('span').last().scope();
console.log(scope.path);  // users
property

scope.id

The unique generated identifier of the scope.

The same value is available as ID.

Reading id
var scope = $('span').last().scope();
console.log(scope.id === scope.ID);  // true
property

scope.element

The element of the scope as a jQuery object.
Reading element
var scope = $('span').last().scope();
console.log(scope.element.prop('tagName'));  // UI-PLUGIN
property

scope.config

The parsed configuration of the element.
Reading config
var scope = $('span').last().scope();
console.log(scope.config.limit);  // 10
property

scope.parent

The parent scope.

The property is undefined for a scope without a parent.

Reading parent
var scope = $('span').last().scope();
console.log(scope.parent.path);  // users
property

scope.isolated

Tells whether the path ignores the parent scope.
Reading isolated
var scope = $('span').last().scope();
console.log(scope.isolated);  // false
property

scope.elements

The elements of the parent scopes and of this scope.

The elements are DOM nodes, and the last one is the element of this scope.

Reading elements
var scope = $('span').last().scope();
console.log(scope.elements.length);  // 2
property

scope.plugin

The Plugin connected to the scope.
Reading plugin
var scope = $('span').last().scope();
console.log(scope.plugin.name);  // users
method

scope.makepath(path)

Resolves a path relative to the scope.
ParameterTypeDescription
path String The path.

Returns String The resolved path.

? is the path of the scope, ?1 is the path of the parent scope and ?2 is the path two levels up. When that level does not exist, the path of the scope is used.

Using makepath()
var scope = $('ui-plugin').scope();
console.log(scope.makepath('?.name'));  // users.name
method

scope.get([path])

Reads a path relative to the scope.
ParameterTypeDescription
path? String The relative path.

Returns Object The value.

Without an argument, the method returns the whole model.

Using get()
var scope = $('ui-plugin').scope();
console.log(scope.get('page'));  // 1
method

scope.set(path, value, [timeout], [reset])

Writes a value to a path relative to the scope.
ParameterTypeDescription
path String The relative path.
value Object The value.
timeout? Number A value of 10 or more delays the change in milliseconds. A lower value is the type of the change.
reset? Boolean true resets the state after a delayed change.
Using set()
var scope = $('ui-plugin').scope();
scope.set('page', 2);
console.log(GET('users.page'));  // 2
method

scope.push(path, value, [timeout], [reset])

Adds an item to an array relative to the scope.
ParameterTypeDescription
path String The relative path.
value Object The item.
timeout? Number A value of 10 or more delays the change in milliseconds. A lower value is the type of the change.
reset? Boolean true resets the state after a delayed change.
Using push()
var scope = $('ui-plugin').scope();
scope.push('items', 'A');
console.log(scope.get('items'));  // ["A"]
method

scope.update([path], [timeout], [reset])

Notifies the listeners of a path relative to the scope.
ParameterTypeDescription
path? String The relative path.
timeout? Number A value of 10 or more delays the change in milliseconds. A lower value is the type of the change.
reset? Boolean true resets the state.
Using update()
var scope = $('ui-plugin').scope();
scope.watch('page', (path, value) => console.log(value));  // 5
window.users.page = 5;
scope.update('page');
method

scope.change([path])

Marks components on a path as touched and modified.
ParameterTypeDescription
path? String The relative path.
Using change()
var scope = $('ui-plugin').scope();
scope.change();
console.log(FIND('field').config.touched);  // true
method

scope.reset([path])

Clears the state of components on a path.
ParameterTypeDescription
path? String The relative path.

The touched and modified state is cleared. The value does not change.

Using reset()
var scope = $('ui-plugin').scope();
var com = FIND('field');
com.modify('Peter');
scope.reset();
console.log(com.config.touched, com.config.modified);  // false false
method

scope.default([path], [timeout])

Restores the default value of the scope.
ParameterTypeDescription
path? String The relative path.
timeout? Number The delay in milliseconds.

The default value comes from the default attribute of the element. A number as the first argument is a delay in milliseconds.

Using default()
var scope = $('ui-plugin').scope();
scope.set('page', 5);
scope.default();
console.log(scope.get('page'));  // 1
method

scope.can()

Tells whether the components in the scope can be saved.

Returns Boolean The result.

The result is true when at least one component is touched and none is invalid. A scope without components returns true.

Using can()
var scope = $('ui-plugin').scope();
FIND('field').modify('Peter');
console.log(scope.can());  // true
method

scope.errors()

Returns the touched and invalid components of the scope.

Returns Array The components.

Using errors()
var scope = $('ui-plugin').scope();
var com = FIND('field');
com.validate = value => !!value;
com.bind('touched');
com.validate2();
console.log(scope.errors().length);  // 1
method

scope.watch(path, callback, [init])

Watches a path relative to the scope.
ParameterTypeDescription
path String The relative path.
callback Function The function that receives the changes.
init? Boolean true calls the callback immediately.

Returns Scope The scope.

The callback receives path, value and type, and this is the scope.

Using watch()
var scope = $('ui-plugin').scope();
scope.watch('page', (path, value) => console.log(path, value));  // users.page 2
scope.set('page', 2);
method

scope.unwatch(path)

Removes the watchers of a path.
ParameterTypeDescription
path String The relative path.

Returns Scope The scope.

The method removes all watchers that this scope has on the path.

Using unwatch()
var scope = $('ui-plugin').scope();
scope.watch('page', () => console.log('called'));
scope.unwatch('page');
scope.set('page', 2);
console.log('end');  // end
method

scope.apply()

Makes the scope active.

Returns Scope The scope.

Functions such as SET() then replace ? with the path of this scope.

Using apply()
var scope = $('ui-plugin').scope();
scope.apply();
SET('?.page', 3);
console.log(GET('users.page'));  // 3
method

scope.FIND(selector, [many], [callback], [timeout])

Finds components inside the scope.
ParameterTypeDescription
selector String The selector of the component.
many? Boolean true returns an array.
callback? Function Waits for the component and receives it.
timeout? Number The longest wait in milliseconds.

Returns Component|Array The result.

The arguments are the same as in the jQuery FIND() extension.

Using FIND()
var scope = $('ui-plugin').scope();
console.log(scope.FIND('field').name);  // field
method

scope.SETTER(selector, method, [...args])

Calls a method of components inside the scope.
ParameterTypeDescription
selector String The selector of the component.
method String The name of the method.
args? Object The arguments.
Using SETTER()
var scope = $('ui-plugin').scope();
scope.SETTER('field/reconfigure', 'title:Orders');
console.log(FIND('field').config.title);  // Orders
method

scope.RECONFIGURE(selector, value)

Reconfigures components inside the scope.
ParameterTypeDescription
selector String The selector of the component.
value String|Object The configuration.
Using RECONFIGURE()
var scope = $('ui-plugin').scope();
scope.RECONFIGURE('field', 'title:Orders');
console.log(FIND('field').config.title);  // Orders
method

scope.find(selector)

Finds nested elements.
ParameterTypeDescription
selector String|Object A selector or an object of selectors.

Returns jQuery|Object The result.

An object of selectors returns an object of jQuery results with the same keys.

Using find()
var scope = $('ui-plugin').scope();
console.log(scope.find('span').length);  // 1
method

scope.closest(selector)

Finds the closest matching parent of the element.
ParameterTypeDescription
selector String The selector.

Returns jQuery The result.

Using closest()
var scope = $('ui-plugin').scope();
console.log(scope.closest('body').length);  // 1
method

scope.append(value)

Adds content to the end of the element.
ParameterTypeDescription
value String|Array The content.

Returns jQuery The element.

An array is joined.

Using append()
var scope = $('ui-plugin').scope();
scope.append('<b>A</b>');
console.log(scope.find('b').text());  // A
method

scope.event(name, [selector], callback)

Attaches a jQuery event to the element.
ParameterTypeDescription
name String The name of the event.
selector? String The delegated selector.
callback Function The handler.

Returns Scope The scope.

The arguments are the same as in the jQuery on() method.

Using event()
var scope = $('ui-plugin').scope();
scope.event('click', 'span', () => console.log('clicked'));  // clicked
scope.find('span').click();
method

scope.attr(name, [value])

Reads or writes an attribute of the element.
ParameterTypeDescription
name String The attribute.
value? String The new value.

Returns String|Scope The value, or the scope when value is passed.

Using attr()
var scope = $('ui-plugin').scope();
scope.attr('title', 'Users');
console.log(scope.attr('title'));  // Users
method

scope.attrd(name, [value])

Reads or writes a data- attribute of the element.
ParameterTypeDescription
name String The name without the data- prefix.
value? String The new value.

Returns String|Scope The value, or the scope when value is passed.

Using attrd()
var scope = $('ui-plugin').scope();
scope.attrd('id', '5');
console.log(scope.attr('data-id'));  // 5
method

scope.attrd2(name)

Reads a data- attribute from the element or its closest parent that has it.
ParameterTypeDescription
name String The name without the data- prefix.

Returns String The value.

Using attrd2()
var scope = $('ui-plugin').scope();
console.log(scope.attrd2('area'));  // main
method

scope.css(name, [value])

Reads or writes a CSS property of the element.
ParameterTypeDescription
name String The property.
value? String The new value.

Returns String|Scope The value, or the scope when value is passed.

Using css()
var scope = $('ui-plugin').scope();
scope.css('display', 'block');
console.log(scope.css('display'));  // block
method

scope.autofocus([selector])

Moves the focus to the first visible field in the element.
ParameterTypeDescription
selector? String The fields. Default: input,select,textarea

Returns Scope The scope.

The method does nothing on mobile devices.

Using autofocus()
var scope = $('ui-plugin').scope();
scope.autofocus();
console.log(document.activeElement.tagName);  // INPUT
method

scope.remove()

Removes the scope, its element and its components.
Using remove()
var scope = $('ui-plugin').scope();
scope.remove();
console.log($('ui-plugin').length);  // 0
Made in Europe