Reputation: 149
Can someone explain to me what this code does?
dojo[(show ? "remove" : "add") + "Class"](this.listNode, "tweetviewHidden");
Here's the function in which this function value belongs to:
// Provide the class
dojo.provide("tweetview._ViewMixin");
// Declare the class
dojo.declare("tweetview._ViewMixin", null, {
// Returns this pane's list
getListNode: function() {
return this.getElements("tweetviewList",this.domNode)[0];
},
// Updates the list widget's state
showListNode: function(show) {
dojo[(show ? "remove" : "add") + "Class"](this.listNode, "tweetviewHidden");
},
// Pushes data into a template - primitive
substitute: function(template,obj) {
return template.replace(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g, function(match,key){
return obj[key];
});
},
// Get elements by CSS class name
getElements: function(cssClass,rootNode) {
return (rootNode || dojo.body()).getElementsByClassName(cssClass);
}
});
Source: http://dojotoolkit.org/documentation/tutorials/1.6/mobile/tweetview/starting_tweetview
Upvotes: 1
Views: 574
Reputation: 14605
It "toggles" the class via the "show" variable. That is, if "show" is truthy, then the class will be removed from the node, otherwise it will be added to the node.
Consider using the following shortcut:
dojo.toggleClass(this.listNode, "tweetviewHidden", !show);
Upvotes: 0
Reputation:
It uses the []
brackets and the ?:
ternary operator, to do something like this:
if(show){
dojo.removeClass(this.listNode, "tweetviewHidden");
}else{
dojo.addClass(this.listNode, "tweetviewHidden");
}
Upvotes: 1
Reputation: 5015
quite simple, if show is true, it will call dojo.removeClass(this.listNode, "tweetviewHidden");
and if its false, it will call dojo.addClass(this.listNode, "tweetviewHidden");
.
Essentially its a toggling function.
the [ ] brackets open up an object to access the value by key. just like var bla={"foo":"bar"}; bla["foo"];
. Now, since its dojo, the value is a function, which will be executed
Upvotes: 2
Reputation: 476970
Slightly more verbosely, the code does something like this:
if (show) { f = dojo["removeClass"]; }
else { f = dojo["addClass"]; }
f(this.listNode, "tweetviewHidden");
I suppose dojo
acts as a container for functions that can be looked up by name via []
.
Upvotes: 1