The library exports three functions: _e(), _t(), _c().
_e() queries and wraps DOM elements into an array,
providing methods to take part of this array,
read attributes and work with CSS classes,
alongside with all the default Array() methods.
_t() is a small and quite simple templating function.
It allows to process string templates,
fill in HTML forms with values from an object and take user-provided values back
without any need for extra coding.
For example, you can fetch a record from a database as an object,
pass it to this function, and after user is done with editing,
take all the values as an object with one call and write it back.
_c() controls all the aspects of web page behavior not covered with the previous two.
AJAX requests, keyboard interaction, value checking are obvious essentials;
bi-directional value binding is a powerful tool to bind the value of an HTML field with a value of a Javascript object.
_e function performs querying of DOM elements and wraps them in an array,
provides methods to iterate over the array, access attributes and CSS classes of that elements.
Wraps a single DOM element into array to give access to methods of the library
Returns an array of elements that are matched by any of the specified selectors that implements a set of functions to iterate over elements:
Returns even elements in the array
Returns odd elements in the array
Returns a new array containing n first elements of the array
Returns a new array containing n last elements of the array
Unwraps and returns the first DOM element in the array
Returns context-dependent value of the first element (true/false for checkbox INPUT elements, .value attribute value for INPUT, SELECT, TEXTAREA elements, .innerHTML for the rest elements)
Sets the value of the first element (.checked attribute of checkbox INPUT elements, .value attribute of INPUT, SELECT, TEXTAREA elements, .innerHTML of the rest elements)
Returns an object with attributes of the first element
Returns value of attributeName attribute or undefined if the attribute doesn’t exist
This function applies CSS classes in classValue string or array to all elements in the array
Returns an array that implements methods to manipulate CSS classes of an element:
Adds class className to the first element’s .class attribute
Returns the same array of CSS classes, allows chaining
Removes class className from the first element’s .class attribute
Returns the same array of CSS classes, allows chaining
Adds className if .class attribute of the first element doesn’t contain it, removes otherwise
Returns the same array of CSS classes, allows chaining
Returns true if .class attribute contains className, false otherwise
_t function provides templating for the library
It’s a simple function handy when you need to add values to a string, for example:
_t('Hello {username}!', {username: 'Jerry'}) will return Hello, Jerry!
A more complex approach to templating
For example, HTML code for template:
<div id="form-fields">
<input field-name="username" type="text" />
prefers
<select field-name="prefers">
<option></option>
</select>
</div>
Can be filled in with values this way:
_t(_e('form-fields').node, 'field-name', {username: '', prefers: ['tea', 'coffee']})
Returns an object with form values, for the example above, after user enters his name and selects what he prefers:
<div id="form-fields">
<input field-name="username" type="text" value="John" />
prefers
<select field-name="prefers">
<option>tea</option>
<option selected>coffee</option>
</select>
</div>
The function returns:
_t(_e('form-fields).node, 'field-name') -> {username: 'John', prefers: 'coffee'}
_c function provides general control over page behavior
propertyNamecallback(newValue)Performs bi-directional binding between object.propertyName and element values;
whenever object.propertyName changes, element value changes as well,
when user changes the element value, object.propertyName value reflects the change. In both cases callback(newValue) is called.
The function merges two arrays, for example:
_c.merge([1, 2], [2, 3]) -> [1, 2, 3]
_c.merge([1, 2], [2, 3], false) -> [1]
_c.merge([1, 2], [2, 3], true) -> [1, 3]
The function merges two objects, for example:
var o1 = {a: 1, b: 2},
o2 = {b: 3, c: 4}
_c.merge(o1, o2) -> {a: 1, b: 2, c: 4}
_c.merge(o1, o2, true) -> {a: 1, b: 3, c: 4}
Makes a deep copy of an array
Makes a deep copy of an object
hasOwnProperty alias
Compare keyboard event with a human readable keyboard combination, for example:
window.onkeydown = function(e) {
_c.keys(e, 'ctrl+s') // returns true when user presses Ctrl + S combination
}
callback(responseObject, responseString, requestObject) where
Performs GET request
callback(responseObject, responseString, requestObject) where
Performs POST request
callback(responseObject, responseString, requestObject) where
Performs PUT request
callback(responseObject, responseString, requestObject) where
Performs DELETE request
Returns true if value is an object
Returns true if value is an array
Returns true if value is a string
Returns true if value is a number