vConsole provides some useful helper functions for efficient plugin development.
Helper functions are mounted in different vConsole properties according to their usage:
vConsole.tool
: Helper functions.vConsole.$
: DOM-related functions.
Check whether a value is a certain type.
- Boolean
Encode a text into a HTML non-sensitive string.
- (required) text: A string to be encoded.
- String
Set data to localStorage
. A prefix vConsole_
will be added to key
automatically.
Note that some devices may not have localStorage
and then value
would not be saved under this situation, so DO NOT use this method to save permanent data.
- (required) key: A string, the name of data.
- (required) value: A string, the value of data.
- None
vConsole.tool.setStorage('count', 1);
Get data from localStorage
. A prefix vConsole_
will be added to key
automatically.
- (required) key: A string, the name of data.
- String, the value of data.
var num = vConsole.tool.setStorage('count'); // => 1
Returns the first element within the document or baseElement that matches the specified group of selectors.
- (required) selectors: A string containing one or more CSS selectors separated by commas.
- (optional) baseElement: An element object, default to be
document
.
- Element object
var $page = vConsole.$.one('#my_page');
var $item = vConsole.$.one('.item', $page);
Returns a list of elements within the document or baseElement that matches the specified group of selectors.
- (required) selectors: A string containing one or more CSS selectors separated by commas.
- (optional) baseElement: An element object, default to be
document
.
- Element object
var $page = vConsole.$.one('#my_page');
var $items = vConsole.$.all('.item', $page);
Add the specified class(es) to element(s).
- (required) elements: A single or a list of element object(s).
- (required) className: A string of one or more space-separated classes.
- None
var $items = vConsole.$.all('.item');
vConsole.$.addClass($items, 'selected');
Remove the specified class(es) of element(s).
- (required) elements: A single or a list of element object(s).
- (required) className: A string of one or more space-separated classes.
- None
var $items = vConsole.$.all('.item');
vConsole.$.removeClass($items, 'selected');
Check whether an element is assigned the given class.
- (required) element: An element object.
- (required) className: A string.
- Boolean
var $page = vConsole.$.one('#my_page');
if (vConsole.$.hasClass($page, 'actived')) {
// do something
}
Bind an event to element(s).
- (required) elements: A single or a list of element object(s).
- (required) eventType: A string of event's type.
- (required) fn: A function to execute when the event is triggered.
- (optional) useCapture: A boolean that indicates the event uses capturing or bubbling, default to be
false
.
- None
var $btn = vConsole.$.one('#submit');
vConsole.$.bind($btn, 'click', function(event) {
event.preventDefault();
alert('submit!');
});
Bind an event to an element, and only this element's descendants which match the selectors can trigger the event.
- (required) element: An element object.
- (required) eventType: A string of event's type.
- (required) selectors: A string containing one or more CSS selectors separated by commas.
- (required) fn: A function to execute when the event is triggered.
- None
var $page = vConsole.$.one('#my_page');
vConsole.$.delegate($page, 'click', '.item', function(event) {
vConsole.$.addClass(this, 'selected'); // this => '.item'
});
Compile a template into an element object or a HTML string with given data.
- (required) tpl: A template string.
- (required) data: A key-value data which is used to render the template.
- (optional) toString: A boolean that indicates whether returns an element object or a HTML string, default to be
false
.
- Element object or HTML string
If:
{{if}}
...
{{else}}
...
{{/if}}
For:
{{for (var i=0; i<10; i++)}}
...
{{continue}}
{{break}}
{{/for}}
Switch:
{{switch (flag)}}
{{case 1}}
...
{{break}}
{{default}}
...
{{/switch}}
Print:
{{flag}}
var tpl = '<ul>' +
'{{for (var i = 0; i < list.length; i++)}}' +
'<li>' + '{{list[i]}}' + '</li>' +
'{{/for}}' +
'</ul>';
var data = {
list: ['Red', 'Blue', 'Yellow']
};
var html = vConsole.$.render(tpl, data, true);
document.body.innerHTML += html;
Output:
<ul>
<li>Red</li>
<li>Blue</li>
<li>Yellow</li>
</ul>