ui-component
Die Attribute, Konfigurationsschlüssel, Methoden und CSS-Klassen des Elements ui-component.
Auf dieser Seite
Das Element <ui-component> platziert eine mit COMPONENT() deklarierte Komponente auf der Seite. Seine Attribute wählen die Komponente aus, binden sie an einen Path und konfigurieren sie. Komponenten verwenden erklärt den Umgang mit dem Element, und die dabei erzeugte Instanz ist unter Komponente beschrieben.
API
property
name="component"
Wählt die zu erstellende Komponente aus.
name="component"Das Attribut akzeptiert diese Formen:
| Wert | Bedeutung |
|---|---|
box | Die als box deklarierte Komponente. |
box@2 | Die als box@2 deklarierte Komponente. |
box|panel | Die erste der aufgeführten Komponenten, die deklariert ist. |
box,panel | Je eine Instanz jeder Komponente auf demselben Element. |
lazy box | Die Instanz wird beim ersten SETTER()-Aufruf für box erstellt. |
Ein Element ohne name wird ignoriert.
<ui-component name="hello" path="common.user"></ui-component>
<script>
COMPONENT('hello', function(self) {
self.setter = function(value) {
self.text('Hello ' + value);
};
});
SET('common.user', 'Peter');
</script>
<ui-component name="missing|hello"></ui-component>
<script>
COMPONENT('hello', function(self) {
console.log(self.name); // hello
});
</script>
<ui-component name="lazy dialog"></ui-component>
<script>
COMPONENT('dialog', function(self) {
self.show = function(title) {
console.log('show', title); // show Welcome
};
});
// The first SETTER() call creates the instance
setTimeout(() => SETTER('dialog/show', 'Welcome'), 500);
</script>
property
path="path"
Bindet die Komponente an einen Path des Datenmodells.
path="path"Der Wert des Paths wird an den Delegate setter übergeben. Das Attribut akzeptiert diese Formen:
| Wert | Bedeutung |
|---|---|
form.name | Ein absoluter Path. |
?.name | Ein Path relativ zum nächstgelegenen ui-plugin. |
? | Der Path des nächstgelegenen <ui-plugin>. |
*name | Ein Path innerhalb von DEF.pathcommon, standardmäßig common.. |
%name | Ein temporärer Path. |
@key | Liest den Wert, der mit data() der übergeordneten Komponente gespeichert wurde. |
form.name --> expression | Wandelt den Wert mit einem Ausdruck um, der value erhält. |
Eine Komponente ohne path ist nicht an das Datenmodell gebunden. Mehr zur Syntax findest du unter Paths.
<ui-plugin path="users">
<ui-component name="show" path="?.name"></ui-component>
</ui-plugin>
<ui-component name="show" path="*title"></ui-component>
<script>
PLUGIN('users', function(exports) {});
COMPONENT('show', function(self) {
console.log(self.path);
});
</script>
<ui-component name="upper" path="form.name --> (value || '').toUpperCase()"></ui-component>
<script>
COMPONENT('upper', function(self) {
self.setter = function(value) {
self.text(value);
};
});
SET('form.name', 'peter');
</script>
<ui-component name="card">
<ui-component name="label" path="@title"></ui-component>
</ui-component>
<script>
COMPONENT('card', function(self) {
self.make = function() {
self.data('title', 'Orders');
};
});
COMPONENT('label', function(self) {
self.setter = function(value) {
self.text(value);
};
});
setTimeout(() => FIND('card').data('title', 'Invoices'), 500);
</script>
property
config="key:value;key:value"
Legt die Anfangskonfiguration der Instanz fest.
config="key:value;key:value"Der Wert ist eine Liste von key:value-Paaren, getrennt durch Semikolons. Zahlen und boolesche Werte werden in ihre Typen umgewandelt. Das Attribut wird einmal gelesen, wenn die Instanz erstellt wird. Um die Konfiguration später zu ändern, rufst du reconfigure() auf.
Die Konfiguration wird in dieser Reihenfolge zusammengeführt: der Standard aus COMPONENT(), dieses Attribut und danach die Regeln aus CONFIG().
<ui-component name="box" config="title:Orders;limit:10;visible:true"></ui-component>
<script>
COMPONENT('box', 'limit:5;color:red', function(self, config) {
console.log(config.title, config.limit, config.visible, config.color); // Orders 10 true red
});
</script>
type
config="=key:path"
Ein Schlüssel, der mit = beginnt, liest seinen Wert aus einem Path und folgt dessen Änderungen.
config="=key:path"Der Wert von =key:path wird aus dem Path gelesen. Ändert sich der Path, erhält die Instanz den neuen Wert über den Delegate configure.
<ui-component name="box" config="=title:common.title"></ui-component>
<script>
SET('common.title', 'First');
COMPONENT('box', function(self, config) {
self.configure = function(key, value) {
key === 'title' && self.text(value);
};
});
setTimeout(() => SET('common.title', 'Second'), 500);
</script>
type
config="=path"
Ein Wert, der mit = beginnt und keinen Doppelpunkt enthält, lädt die gesamte Konfiguration aus einem Path.
config="=path"Der Path muss ein Objekt enthalten. Die Instanz wird bei jeder Änderung des Paths neu konfiguriert.
<ui-component name="box" config="=common.options"></ui-component>
<script>
SET('common.options', { title: 'Orders' });
COMPONENT('box', function(self, config) {
self.configure = function(key, value) {
key === 'title' && self.text(value);
};
});
setTimeout(() => SET('common.options', { title: 'Invoices' }), 500);
</script>
type
config="%variable"
Ein Wert, der mit % beginnt, verwendet eine globale Variable als Konfigurationsobjekt.
config="%variable"Die Instanz arbeitet direkt mit dem in window gespeicherten Objekt.
<ui-component name="box" config="%boxoptions"></ui-component>
<script>
window.boxoptions = { title: 'Orders' };
COMPONENT('box', function(self, config) {
console.log(config.title, config === window.boxoptions); // Orders true
});
</script>
type
config="key:@xs=value,@lg=value"
Ein Wert kann sich je nach Anzeigegröße oder heller und dunkler Darstellung unterscheiden.
config="key:@xs=value,@lg=value"Schreibe die Alternativen als @name=value, getrennt durch Kommas. Die Namen sind xs, sm, md, lg, dark und light. Verwendet wird der Wert, der zur aktuellen Anzeige passt. Hat die aktuelle Größe keinen Wert, gilt die nächstgelegene definierte Größe.
<ui-component name="grid" config="columns:@xs=1,@sm=2,@md=3,@lg=4"></ui-component>
<script>
COMPONENT('grid', function(self, config) {
console.log(WIDTH(), config.columns); // xs 1
});
</script>
config
$id:identifier
Legt die Kennung der Instanz fest.
$id:identifierDie Kennung wird in id gespeichert und lässt sich in Selektoren wie #users verwenden.
<ui-component name="box" config="$id:users"></ui-component>
<script>
COMPONENT('box', function(self) {});
setTimeout(() => console.log(FIND('#users').name), 500); // box
</script>
config
$type:type
Legt den Datentyp der Instanz fest.
$type:typeDer Typ wird in type gespeichert und an Parser und Formatter übergeben.
<ui-component name="box" config="$type:number"></ui-component>
<script>
COMPONENT('box', function(self) {
self.make = () => console.log(self.type); // number
});
</script>
config
$class:classes
Schaltet CSS-Klassen auf dem Element um.
$class:classes<ui-component name="box" config="$class:highlighted"></ui-component>
<script>COMPONENT('box', function(self) {});</script>
config
$assign:path
Schreibt die Instanz in einen Path.
$assign:pathDer Path wird auf null gesetzt, wenn die Instanz zerstört wird.
<ui-component name="box" config="$assign:common.box"></ui-component>
<script>
COMPONENT('box', function(self) {});
setTimeout(() => console.log(GET('common.box').name), 500); // box
</script>
config
$init:path
Ruft eine Funktion auf, wenn die Instanz bereit ist.
$init:pathDie Funktion erhält die Instanz, und auch this ist die Instanz.
<ui-component name="box" config="$init:common.onbox"></ui-component>
<script>
window.common = { onbox: instance => console.log('ready', instance.name) }; // ready box
COMPONENT('box', function(self) {});
</script>
config
$setter:path
Ruft jedes Mal eine Funktion auf, wenn die Instanz einen Wert erhält.
$setter:pathDie Funktion erhält value, path und type, und this ist die Instanz.
<ui-component name="box" path="form.name" config="$setter:common.onvalue"></ui-component>
<script>
window.common = { onvalue: value => value && console.log(value) }; // Peter
COMPONENT('box', function(self) {
self.setter = function() {};
});
SET('form.name', 'Peter');
</script>
config
$state:path
Ruft jedes Mal eine Funktion auf, wenn sich der Zustand der Instanz ändert.
$state:pathDie Funktion erhält type und what, also dieselben Argumente wie der Delegate state.
<ui-component name="box" config="$state:common.onstate"></ui-component>
<script>
window.common = { onstate: (type, what) => console.log(type, what) }; // 0 3
COMPONENT('box', function(self) {});
</script>
config
$noscope:true
Hält den Path außerhalb des nächstgelegenen <ui-plugin>.
$noscope:trueDas Zeichen ? im Path wird nicht ersetzt.
<ui-plugin path="users">
<ui-component name="box" path="?.name" config="$noscope:true"></ui-component>
</ui-plugin>
<script>
PLUGIN('users', function(exports) {});
COMPONENT('box', function(self) {
console.log(self.path); // ?.name
});
</script>
config
$bindvisible:true
Verzögert Werte, bis das Element sichtbar ist.
$bindvisible:trueDas hat dieselbe Wirkung wie der Aufruf von bindvisible() in der Deklaration.
<div id="tab" style="display:none">
<ui-component name="box" path="form.name" config="$bindvisible:true"></ui-component>
</div>
<script>
COMPONENT('box', function(self) {
self.setter = function(value) {
value && console.log('received', value); // received Peter
};
});
SET('form.name', 'Peter');
setTimeout(() => $('#tab').show(), 500);
</script>
config
$url:url
Importiert die Komponente, wenn sie nicht deklariert ist.
$url:urlDie URL wird an IMPORT() übergeben. Der Import läuft einmal pro URL.
<ui-component name="remote" config="$url:/components/remote.html"></ui-component>
config
$realtime:false
Legt fest, wann ein Eingabefeld mit data-jc-bind seinen Wert schreibt.
$realtime:falseMit true wird der Wert geschrieben, während die Person tippt. Mit false wird er beim Event change geschrieben.
<ui-component name="box" path="form.name" config="$realtime:false">
<input data-jc-bind />
</ui-component>
<script>
COMPONENT('box', function(self) {});
setTimeout(function() {
var input = $('input').val('Peter').trigger('input');
setTimeout(function() {
console.log(GET('form.name') || 'empty'); // empty
input.trigger('change');
console.log(GET('form.name')); // Peter
}, 300);
}, 500);
</script>
config
$delay:milliseconds
Legt die Verzögerung der Echtzeit-Bindung in Millisekunden fest.
$delay:millisecondsDer Standardwert ist DEF.delaykeypress, also 200.
<ui-component name="box" path="form.name" config="$delay:20">
<input data-jc-bind />
</ui-component>
<script>
COMPONENT('box', function(self) {});
setTimeout(function() {
$('input').val('Peter').trigger('input');
setTimeout(() => console.log(GET('form.name')), 60); // Peter
}, 500);
</script>
property
default="expression"
Legt den Standardwert des Paths fest.
default="expression"Das Attribut enthält einen JavaScript-Ausdruck. Der Wert wird geschrieben, wenn der Path undefined ist, und default() stellt ihn wieder her.
<ui-component name="box" path="form.name" default="'Anonymous'"></ui-component>
<script>
COMPONENT('box', function(self) {
self.setter = function() {};
});
setTimeout(() => console.log(GET('form.name')), 500); // Anonymous
</script>
property
plugin="path"
Erstellt einen Scope direkt auf dem Element.
plugin="path"Der Wert ist der Path des Scopes. Das funktioniert so, als würdest du das Element in ein ui-plugin einschließen.
<ui-component name="box" plugin="users" path="?.name"></ui-component>
<script>
PLUGIN('users', function(exports) {});
COMPONENT('box', function(self) {
console.log(self.path); // users.name
});
</script>
property
data-jc-path="path"
Legt den Path fest und hat Vorrang vor dem Attribut path.
data-jc-path="path"<ui-component name="box" path="form.a" data-jc-path="form.b"></ui-component>
<script>
COMPONENT('box', function(self) {
console.log(self.path); // form.b
});
</script>
property
data-jc-config="key:value"
Legt die Konfiguration fest und hat Vorrang vor dem Attribut config.
data-jc-config="key:value"<ui-component name="box" config="title:A" data-jc-config="title:B"></ui-component>
<script>
COMPONENT('box', function(self, config) {
console.log(config.title); // B
});
</script>
property
data-jc-class="classes"
Schaltet CSS-Klassen um, wenn die Instanz bereit ist.
data-jc-class="classes"Trenne mehrere Klassen durch ein Leerzeichen.
<ui-component name="box" data-jc-class="ready"></ui-component>
<script>COMPONENT('box', function(self) {});</script>
property
data-jc-noscope="true"
Hält den Path außerhalb des nächstgelegenen <ui-plugin>.
data-jc-noscope="true"Das hat dieselbe Wirkung wie der Schlüssel $noscope.
<ui-plugin path="users">
<ui-component name="box" path="?.name" data-jc-noscope="true"></ui-component>
</ui-plugin>
<script>
PLUGIN('users', function(exports) {});
COMPONENT('box', function(self) {
console.log(self.path); // ?.name
});
</script>
method
element.get()
Gibt den Wert des gebundenen Paths zurück.
element.get()Rückgabe Object Der Wert des Paths.
var element = document.querySelector('ui-component');
console.log(element.get()); // Peter
method
element.set(value, [type])
Schreibt einen Wert in den gebundenen Path.
element.set(value, [type])| Parameter | Typ | Beschreibung |
|---|---|---|
value |
Object |
Der neue Wert. |
type? |
Number |
Der an den Setter übergebene Typ der Aktualisierung. |
Die Instanz wird als geändert markiert.
var element = document.querySelector('ui-component');
element.set('Anna');
console.log(GET('form.name')); // Anna
method
element.update([type])
Benachrichtigt den gebundenen Path mit seinem aktuellen Wert.
element.update([type])| Parameter | Typ | Beschreibung |
|---|---|---|
type? |
Number |
Der an den Setter übergebene Typ der Aktualisierung. |
Die Instanz wird als geändert markiert.
var element = document.querySelector('ui-component');
window.form.name = 'Anna';
element.update();
setTimeout(() => console.log(element.textContent), 50); // Anna
method
element.modify(value, [type])
Schreibt einen Wert über die Parser der Instanz.
element.modify(value, [type])| Parameter | Typ | Beschreibung |
|---|---|---|
value |
Object |
Der neue Wert. |
type? |
Number |
Der Typ der Aktualisierung. Standard: 2 |
Die Methode ruft modify() der Instanz auf.
var element = document.querySelector('ui-component');
element.modify(' Anna ');
console.log(GET('form.name')); // Anna
method
element.reconfigure([value], [init], [noemit])
Ändert die Konfiguration der Instanz.
element.reconfigure([value], [init], [noemit])| Parameter | Typ | Beschreibung |
|---|---|---|
value? |
String|Object |
Die neue Konfiguration. |
init? |
Boolean |
Übergibt init an den Delegate configure. |
noemit? |
Boolean |
Überspringt den Delegate configure. |
Ohne Wert liest die Methode das Attribut config erneut.
var element = document.querySelector('ui-component');
element.reconfigure('title:Invoices');
console.log(element.config.title); // Invoices
method
element.reset()
Löscht den Zustand „ungültig“ und „geändert“ der Instanz.
element.reset()var element = document.querySelector('ui-component');
element.set('Anna');
element.reset();
console.log(element.config.modified); // false
property
element.config
Das Konfigurationsobjekt der Instanz.
element.configDie Eigenschaft existiert, sobald die Instanz erstellt wurde.
var element = document.querySelector('ui-component');
console.log(element.config.title); // Orders
delegate
element.setter = function(value, path, type)
Eine Funktion auf dem DOM-Element, die jeden Wert erhält.
element.setter = function(value, path, type)Weise sie dem Element zu. Die Funktion läuft nach dem Delegate setter der Instanz und erhält dieselben Argumente.
var element = document.querySelector('ui-component');
element.setter = function(value, path, type) {
console.log(value, path, type); // Anna form.name 1
};
SET('form.name', 'Anna');
delegate
element.state = function(config)
Eine Funktion auf dem DOM-Element, die bei einer Zustandsänderung läuft.
element.state = function(config)Die Funktion erhält das Konfigurationsobjekt der Instanz.
var element = document.querySelector('ui-component');
element.state = function(config) {
console.log(config.touched); // true
};
FIND('demo').bind('touched');
delegate
element.configure = function(key, value, init, prev)
Eine Funktion auf dem DOM-Element, die Konfigurationsänderungen erhält.
element.configure = function(key, value, init, prev)Die Funktion erhält dieselben Argumente wie der Delegate configure.
var element = document.querySelector('ui-component');
element.configure = function(key, value, init, prev) {
console.log(key, value, prev); // title Invoices Orders
};
element.reconfigure('title:Invoices');
event
$(element).on('component', callback)
Ein jQuery-Event, das auf dem Element ausgelöst wird, wenn die Instanz bereit ist.
$(element).on('component', callback)Das Event wird einmal ausgelöst.
<ui-component name="box" id="box"></ui-component>
<script>
COMPONENT('box', function(self) {});
$('#box').on('component', () => console.log('ready')); // ready
</script>
class
.ui-modified
Vorhanden, solange der Wert der Instanz geändert ist.
.ui-modifiedDie Klasse folgt config.modified.
var com = FIND('demo');
com.bind('modified');
console.log(com.element.hclass('ui-modified')); // true
class
.ui-touched
Vorhanden, nachdem mit der Instanz interagiert wurde.
.ui-touchedDie Klasse folgt config.touched.
var com = FIND('demo');
com.bind('touched');
console.log(com.element.hclass('ui-touched')); // true
class
.ui-invalid
Vorhanden, solange der Wert der Instanz ungültig ist.
.ui-invalidDie Klasse folgt config.invalid, das vom Delegate validate gesetzt wird.
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self) {
self.validate = value => !!value;
});
</script>
class
.ui-disabled
Vorhanden, solange die Instanz deaktiviert ist.
.ui-disabledDie Klasse folgt config.disabled. Der Befehl disabled von <ui-bind> setzt den Schlüssel.
<ui-bind path="form.locked" config="disabled:value">
<ui-component name="box"></ui-component>
</ui-bind>
<script>
COMPONENT('box', function(self) {});
SET('form.locked', true);
</script>