just.View(optionsnullable)
Templarize elements easily.
Parameters:
Name | Type | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options [nullable] | object | Any just.View property.Properties
|
Example
Members
attributes :object
Updatable attributes. I.e: attributes that will be updated when just.View#update gets called.
Properties:
Name | Type | Default | Description |
---|---|---|---|
var [optional] | string | ${prefix} | The attribute for text replacements. |
html [optional] | string | ${prefix}-html | The attribute for html replacements. |
attr [optional] | string | ${prefix}-attr | The attribute for attribute replacements. |
if [optional] | string | ${prefix}-if | The attribute for conditional/if replacements. |
as [optional] | string | ${prefix}-as | The attribute for alias replacements. Scoping is not supported yet. |
for [optional] | string | ${prefix}-for | The attribute for loops replacements. Only arrays are supported now. |
on [optional] | string | ${prefix}-on | The attribute for listener replacements. |
Type:
object
Methods
attachListeners(element, datanon-null, attributeNamenon-null) → {object}
Parse an attribute as a json and set keys as event names/types and values as listeners. Values are accessable properties that require a function as final value.
Parameters:
Name | Type | Description |
---|---|---|
element | Node | The target element. |
data | object | Data for the accessable properties, containing the listeners. |
attributeName | string | Name of the attribute that contains the parseable json. |
Returns:
The attached listeners, with event.types as keys.
- Type
- object
getAliases(element, datanullable, attributeNamenullable) → {object}
Define aliases using a stringified JSON from an element attribute; access object values and set keys as alias.
Parameters:
Name | Type | Description |
---|---|---|
element | Element | The target element. |
data [nullable] | object | Some object. |
attributeName [nullable] | string | The name for attribute containing a stringified json. |
Returns:
An object containing keys as alias.
- Type
- object
init(options) → {Array.<View>}
Find elements with the just.View.INIT_ATTRIBUTE_NAME attribute, parse its value as json, and call just.View with those options.
Parameters:
Name | Type | Description | ||||||
---|---|---|---|---|---|---|---|---|
options | object | Properties
|
Returns:
The created views.
- Type
- View[]
Example
replaceVars(value, datanullable, defaultValue) → {string}
Replace placeholders (
${}
, eg. ${deep.deeper}
) within a string.Parameters:
Name | Type | Description |
---|---|---|
value | string or object | Some text or an object. If an object is given, it will just.View.resolveConditionals first, then replace ${placeholders} within the accessed value. |
data [nullable] | object | An object containing the data to be replaced. |
defaultValue | Any | By default, it skips replacements if some accessed value is undefined. Any other value will be stringified (returned to the String#replace function). |
Returns:
The replaced string. If some value is undefined, it won't be replaced at all.
- Type
- string
Examples
resolveConditional(condititionalnullable) → {*|boolean}
Access to an object and return its value.
Parameters:
Name | Type | Description |
---|---|---|
condititional [nullable] | string | Expected keys splitted by ".". Use "!" to negate a expression. Use "true" to return true. |
Returns:
if the conditional is negated, a boolean. Else, the accessed value.
- Type
- Any or boolean
resolveConditionals(conditionals, datanullable) → {*}
Access to multiple conditionals and return the first value that is truthy.
Parameters:
Name | Type | Description |
---|---|---|
conditionals | object or string | An object containing conditions (expected properties) as keys, or a string containing a condition (a expected property). |
data [nullable] | object | An object with all the properties. |
Returns:
View.resolveConditional()'s returned value.'
- Type
- Any
updateAttributes(element, datanullable, attributeNamenullable) → {boolean}
Create dynamic attributes after replacing variables in values. Please note that null/undefined values won't be replaced.
Parameters:
Name | Type | Description |
---|---|---|
element | Element | The target element. |
data [nullable] | object | Some object. |
attributeName [nullable] | string | The name for attribute containing a stringified json. |
Returns:
true if the attribute contains some value, false otherwise.
- Type
- boolean
updateConditionals(element, datanullable, attributeName) → {boolean}
Show/Hide an element (by setting/removing the [hidden] attribute) after evaluating the conditional found in the given attribute.
Parameters:
Name | Type | Description |
---|---|---|
element | Element | The target element. |
data [nullable] | object | Some object. |
attributeName | string | The name for the queried attribute. |
Returns:
True if resolved (and hidden), false otherwise.
- Type
- boolean
updateHtmlVars(element, datanullable, attributeNamenullable) → {boolean}
Update the element's markup using just.View.updateVars and
element.innerHTML
.Parameters:
Name | Type | Description |
---|---|---|
element | Element | The target element. |
data [nullable] | object | Some object. |
attributeName [nullable] | string | The name for the queried attribute. |
Returns:
true if the value was updated, false otherwise.
- Type
- boolean
updateLoops(element, data, attributeName) → {Array.<View>}
Iterate over an array to create multiple elements based on a given template (element), append them in order, and update each generated element.
Parameters:
Name | Type | Description |
---|---|---|
element | Node | The target element. |
data | Object | The data. |
attributeName | string | The attribute containing the loop expression. |
Returns:
The updated views or null.
- Type
- View[]
updateVars(element, datanullable, attributeNamenullable, setteropt, nullable) → {boolean}
Update the element's text if the attribute's value is different from the accessed value.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
element | Element | The target element. | |
data [nullable] | object | Some object. | |
attributeName [nullable] | string | The name for the queried attribute. | |
setter [optional] [nullable] | just.View~updateVars_setter | element.textContent | If set, a function to update the element's text. Expects a boolean to be returned. Else, element.textContent will be used to set the updated value and return true. |
Returns:
true if the value was updated, false otherwise. Any other value will be casted to a Boolean.
- Type
- boolean
append(containernullable)
Call just.View#insert to perform an append of the current template element.
Parameters:
Name | Type | Description |
---|---|---|
container [nullable] | Node |
attachListeners(listenersnullable)
Parameters:
Name | Type | Description |
---|---|---|
listeners [nullable] | object | An object in the format: {eventType: fn} . |
create()
Create a clone of an element, remove the .template class, the [hidden] attribute, and the [id] after setting it as a class on the clon. Set just.View#element to the new clon and return the current instance.
Throws:
if the template is not a Node.
- Type
- TypeError
getData(currentDatanon-null) → {object}
Merges all available data into one object in the following order: just.View.globals, just.View#data, currentData, and aliases.
Parameters:
Name | Type | Description |
---|---|---|
currentData | object | It merges this object after globals, and locals, and before setting aliases. |
Returns:
- Type
- object
getUpdatables(container|documentopt, nullable) → {Array.<Node>}
Find all elements that contain a supported attribute and return them.
Parameters:
Name | Type | Description |
---|---|---|
container|document [optional] [nullable] | Node |
Returns:
All matching elements within the given container
- Type
- Node[]
insert(position, containernullable)
Insert just.View#element at the given position into the given container.
Parameters:
Name | Type | Description |
---|---|---|
position | string or object.<{before: Node}> | - "before" will insert the element before the first child. - {"before": Node} will insert the element before the given Node. - else (other values): will use appendChild() by default. |
container [nullable] | Node | The Node that will contain the element. |
Throws:
if a container can't be guessed.
- Type
- TypeError
prepend(containernullable)
Call just.View#insert to perform an prepend of the current template element, at the beginning.
Parameters:
Name | Type | Description |
---|---|---|
container [nullable] | Node |
refresh(newData, skip)
Update the view using just.View#previousData (set on update) and newData. Useful to update the view with previous values or update only some properties, after a normal update.
Parameters:
Name | Type | Description |
---|---|---|
newData | Any | Any new data. |
skip | just.View#update~skip | skip argument. |
reset()
Restore constructor to its default value. Use the #original property to restore it.
update(data, skipnullable)
Update all updatable elements by querying them and calling all possible update* functions, like: - just.View.updateConditionals. - just.View.updateAttributes. - just.View.updateHtmlVars. - just.View.updateVars. - just.View.updateLoops. (In that order).
Parameters:
Name | Type | Description |
---|---|---|
data | Any | Data for the update. Merged with just.View#getData(). |
skip [nullable] | function | A function called on each updatable element. if a truthy value is returned, the update won't take place. |
Type Definitions
updateLoops_expression
Expression in the format:
"currentItem in accessed.array[ as updatableAttribute][,[ cache=true]]"
.Where text enclosed in brackets is optional, and:
- currentItem is the property containing the current iteration data.
- accessed.array is a property to be accessed that contains an array as value.
- updatableAttribute is the name of the attribute that will be updated afterwards by View#update.
- Setting cache will update existing elements, using each element as template. By default, this is true because it improves performance, but it also means that new modifications to each element will be replicated. If you set this to false, all generated elements will be removed before updating them, causing new modifications to be removed, but also hurting performance.
Type:
string
Examples
updateLoops_loopData
Loop data. Contains loop data for the current iteration.
Properties:
Name | Type | Description |
---|---|---|
index | number | The current index. |
array | array | The array. |
length | number | The array's length. |
left | number | Left elements' count. |
Type:
object
access(keys, datanullable)
Access to properties using the dot notation. Supports nested function arguments replacements and reserved keywords. See just.View~parseArguments.
Parameters:
Name | Type | Description |
---|---|---|
keys | string | Accessable properties using the dot notation. |
data [nullable] | object | Accessable data. |
Example
parseArguments(string, data) → {Array}
Parse argument-like strings ("a, b, c") and return valid JSON values.
Parameters:
Name | Type | Description |
---|---|---|
string | string | Argument-like string without quotes, like "a, b, c". |
data | object | Accessable data. |
Returns:
Arguments.
- Type
- Array
Example
updateVars_setter(element, text) → {boolean}
A function to set the updated value.
Parameters:
Name | Type | Description |
---|---|---|
element | Element | The target element. |
text | string | The updated text. |
Returns:
true if updated, false otherwise.
- Type
- boolean