dojo query
dojo/queryreturns a list of DOM nodes based on a CSS selector. Introductiondojo/requestand XHR is half of the AJAX story. Once you make a request for data and receive a response,you must change the page. Being able to change the HTML is dependent on locating nodes. To select DOM nodes in JavaScript,you can use the browser’s native DOM API,but it’s verbose and hard to work with,slow and can differ across browser. For example,retrieving all nodes with the class “progressIndicator” uses this code: // list every node with the class "progressIndicator":
var list = [];
var nodes = document.getElementsByTagName("*");
// iterate over every node in the document....SLOOOW
for(var x = 0; x < nodes.length; x++){
// only nodes with the class "progressIndicator":
if(nodes[x].className == "progressIndicator"){
// add to array:
list.push(nodes[x]);
}
}
console.log(list);
dojo/querygives us a more compact way to do it,and it is often faster,particularly as we ask for more sophisticated kinds of relationships. The following is essentially equivalent to our first example: require(["dojo/query"], function(query){
console.log(query(".progressIndicator"));
});
Usage
Requring in the module is all that is needed: function(query){
var nl = query(".someClass");
});
dojo/queryreturns an instance ofNodeList,which is essentially a JavaScript Array that has been decorated with functions that make it easier to utilise. There are extensions of the baseNodeListthat are available to provide additional functionality:
The first argument is theselectorwhich is a CSS selector string that identifies the nodes that need to be retrieved. The second argument is an optionalcontextwhich limits the scope of the selector and only children of the will be considered. This can either be a string representing the node ID or a DOM node. For example: "dojo/query", "dojo/dom"],32); font-weight:bold">function(query, dom){
".someClass",160)">"someId");
// or
var node = dom.byId("someId");
nl node);
});
dojo/queryandNodeListare specifically designed with chaining in mind. Most functions onNodeListreturn an instance ofNodeList. For example: "dojo/NodeList-dom"],32); font-weight:bold">function(query){
query("li").forEach(function(node){
node.innerHTML = "Something";
}).style("color",160)">"red")
.style("fontSize",160)">"12px");
});
Selector Enginesdojo/queryis responsible for loading the appropriate selector engine. There are several different modes whichdojo/querycan run in:
When you are running Dojo in legacy mode (async:false),monospace!important">dojo/querywill run inacmemode. When you are running withasync:truethe default selector engine level iscss3. The summarize,the two alternate selector engines included with Dojo have the following features (which can be selected explicitly or by the module’s CSS level needs):
Specifying the Selector LevelThe selector level can be controlled though various mechanisms. The default selector level can be specified in the build profile (seeDojo Builder). The selector engine can be specified as part of your Dojo configuration: <script data-dojo-config="selectorEngine: 'css2.1',async: true" src="dojo/dojo.js">
</script>
Or: type="text/javascript">
var dojoConfig = {
selectorEngine: "css2.1",
async: true
};
</script>
>
The selector engine level can be specificed as a loader plugin for each module. For example,if the module needed to do a CSS3 level query,you could write: define(["dojo/query!css3"],32); font-weight:bold">function(query){
query(".someClass:last-child").style("red");
});
If Dojo had started with theliteengine,this will ensure that CSS3 support is available,and will loadacmeon older browsers. It is recommended that you use this syntax for modules that explicitly need more complex queries. If your module is using a simpler query,then"dojo/query"or"dojo/query!css2.1"should be used. Selector SupportThe following tables summerize selector engine levels and their support. Standard CSS2 SelectorsThese selectors can be used with any selector engine.
Additional Selectors Supported By Lite EngineThese selectors are not part of CSS2,but are supported by the lite engine,so effectively then can also be used with any specified selector engine.
Standard CSS2.1 SelectorsTo use these selectors,you must specify thecss2.1,monospace!important">css3,oracmeselector engine.
Standard CSS3 Selectorscss3oracmeselector engine.
Alternate Selector EnginesWe can also use other selector engine levels. Both Sizzle and Slick are excellent selector engines that work withdojo/query. AMD/Dojo compatible versions (just wrapped with AMD) are available here:
Once installed,you can use the selector engine module id as specified selector engine level. We could set Sizzle as the query engine for our page: "selectorEngine: 'sizzle/sizzle'" </script>
or set Slick as the engine for a particular module: "dojo/query!slick/Source/slick"],160)">".someClass:custom-pseudo").style("red");
});
Note for cross-domain legacy API usageThis use case should be quite rare,but presents a wrinkle worth noting. When loadingdojo.jscross-domain and electing to use an alternate selector engine not included indojo.jsitself,legacy APIs will not immediately work,since Dojo base does not finish loading until the selector engine is pulled in asynchronously. In this case,it will be necessary to userequire. In a pinch,legacy code can simply be wrapped like so: "dojo"],32); font-weight:bold">function(dojo){
dojo.require(/* ... */);
// etc...
});
Again,this issueonlyaffects use of legacy APIs when a selector engine is used which is not baked intodojo.js. Examples
Example Selector QueriesThe following tables provide example selector queries and what sort of nodes they would select.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |