-
Notifications
You must be signed in to change notification settings - Fork 0
sQuery()
sQuery(query, callback, defaults, custom, parser);
Param | Type | Info |
---|---|---|
query |
* |
The query to retrieve/pass DOM elements - this is the only required parameter |
[callback] |
Function |
Function to call on each element returned from query
|
[defaults] |
Object |
Default configuration (will be merged with custom and passed to callback ) |
[custom] |
Object |
Custom configuration (will be merged with default and passed to callback ) |
[parser] |
Function |
Function to call on the merged default + custom object |
This parameter should be used to determine which HTML Element(s) from the DOM you are interested in. Throughout the Wiki, what you enter as the value for this parameter will be referred to as a Synergy Query.
A Synergy Query can be one of the following:
- A string matching the name of a module - all elements that match the module name will be returned
- A string representing a selector that will be passed to a
document.querySelectorAll
call - An HTML Element - only this element will be returned
- A NodeList - all element nodes in the list will be returned
- An array where the first value will be the
query
- An object which contains a
name
key to be treated as the module name - all elements that match the module name will be returned
<div class="foo" id="bar">...</div>
The below queries would all return the above HTML Element (learn more about the DOMNode
method):
sQuery('foo').DOMNode;
sQuery('.foo').DOMNode;
sQuery(document.getElementById('bar')).DOMNode;
sQuery(document.querySelectorAll('.foo')).DOMNode;
sQuery(['foo']).DOMNode;
sQuery({name: 'foo'}).DOMNode;
In the above example, the module name is foo
. This means that foo
will be used as the namespace for sQuery operations:
sQuery('foo', function(element) {
sQuery(element).addModifier('buzz');
});
Resulting in:
<div class="foo foo-buzz" id="bar">...</div>
If you wanted to achieve the following result:
<div class="foo fizz-buzz" id="bar">...</div>
...you can override the module name like so:
- target the element by passing an array with the first value as the Synergy Query
- choose a new module name by passing it as the second value in the array
sQuery(['foo', 'fizz'], function(element) {
sQuery(element).addModifier('buzz');
});
This parameter should be used to pass a reference to a function that will be called upon each HTML element matched by the query
parameter.
callback(element, options)
Param | Type | Info |
---|---|---|
element |
HTMLElement |
Each HTML Element returned from the query parameter
|
options |
Object |
Object containing deeply merged defaults + custom objects |
<div id="myModule">...</div>
window.someOtherConfigObject = {
someProperty: true;
}
var defaults = {
someProperty: false;
}
sQuery(document.getElementById('myModule'), function(element, options) {
if (options.someProperty) {
element.style.color = red;
}
}, defaults, someOtherConfigObject);
<div id="myModule" style="color: red;">...</div>
This parameter should be used to pass any default configuration for your module - it will be merged with any custom values passed via the optional custom
parameter and passed to the callback
parameter.
<div class="myModule">...</div>
<div class="myModule">...</div>
var defaults = {
someProperty = false;
}
function myModule(custom) {
sQuery('myModule', function(element, options) {
if (options.someProperty) {
element.style.color = red;
}
}, defaults, custom);
}
This parameter should be used to pass any custom configuration for your module - it will be merged with any default values passed via the defaults
parameter and passed to the callback
parameter.
<div class="myModule">...</div>
<div class="myModule">...</div>
var defaults = {
someProperty = false;
}
function myModule(custom) {
sQuery('myModule', function(element, options) {
if (options.someProperty) {
element.style.color = red;
}
}, defaults, custom);
}
myModule({
someProperty: true
});
<div class="myModule" style="color: red;">...</div>
<div class="myModule" style="color: red;">...</div>
This parameter should be used to pass a reference to a function that will be called upon the options
object to create a new object that will be sent to the callback
function.
// Make all strings in `options` uppercase
function parser(options) {
for (var option in options) {
if (typeof option === 'String') {
options[option] = option.toUpperCase();
}
}
return options;
}
var options = {
fizz: 'buzz';
}
sQuery('myModule', function(element, options) {
console.log(options.fizz); // 'BUZZ'
}, options, someOtherConfigObject, parser);
sQuery(query).namespace; // returns namespace of element(s) returned by `query`
sQuery(query).nodes; // returns elements matched by `query`
sQuery(query).node; // returns first element matched by `query`