JavaScript and UI Architecture Best PracticesPresentation Transcript
JavaScript and UI Architecture
Best Practices
Siarhei Barysiuk
s.barysiuk@sam-solutions.net
Our roadmap
Introduction: What will we cover today?
• Coding patterns
JavaScript-specific best practices
• Design patterns
Language independent patterns, UI architecture
Coding patterns
Namespaces
Tip of the day
“Global variables should be avoided in order to
lower the possibility of variable naming
collisions. “
Tip of the day
“Branch some parts of the browser-specific code
during initialization, when the script loads, rather
than during runtime to avoid performance hit.”
Tip of the day
“The lazy definition pattern is very similar to the
previous init-time branching pattern.
The difference is that the branching happens only
when the function is called for the first time.”
Tip of the day
“Instead of having many parameters, you can use
one parameter and make it an object.
The properties of the object are the actual
parameters.”
Configuration object: Usage of configuration object
var MyApp = MyApp || {};
MyApp.namespace(quot;domquot;);
MyApp.dom.Button = function(text, settings) {
var type = settings.type || 'submit';
var font =settings.font ||'Verdana';
//..other props
//some code here
}
Questions?
Private properties and methods
Tip of the day
“Use local variables and methods inside a
constructor to achieve the “private” level of
protection.
Use naming conventions _doInternalOperation to
show that the function is private/protected.”
Private methods and properties: Private method
var MyApp = MyApp || {};
MyApp.namespace(quot;domquot;);
MyApp.dom.Button = function(text, settings) {
//..process properties
function setStyle(element, settings) {
//setting style to element
};
var button = //...
//..
setStyle(button,settings);
this.clone = function() {
var clonedButton = //...
//...
setStyle(clonedButton, settings);
}
//some code here
}
Questions?
Self-executing functions
Tip of the day
“Self-executing functions are especially suitable for
one-off initialization tasks performed when the
script loads.”
Tip of the day
“Pretty convenient way to call several related
methods on one line as if the methods are the links
in a chain.”
Chaining: Usage
var obj = new MyApp.dom.Element('span'); var obj = new MyApp.dom.Element('span');
obj.setText('hello'); obj.setText('hello')
obj.setStyle('color', 'red'); .setStyle('color', 'red')
obj.setStyle('font', 'Verdana'); .setStyle('font', 'Verdana');
document.body.appendChild(obj); document.body.appendChild(obj);
document.body.appendChild(
new MyApp.dom.Element('span')
.setText('hello')
.setStyle('color', 'red')
.setStyle('font', 'Verdana')
);
Questions?
Design patterns
Unobtrusive JavaScript
Unobtrusive JavaScript: Separate JavaScript functionality
<a onclick=quot;doSomething()quot; href=quot;#quot;>Click!</a>
<a href=quot;backuplink.htmlquot; class=quot;doSomethingquot;>Click!</a>
$('a.doSomething').click(function(){
// Do something here!
alert('You did something, woo hoo!');
});
Unobtrusive JavaScript: Never depend on JavaScript
<script type=quot;text/javascriptquot;>
var now = new Date();
if(now.getHours() < 12)
document.write('Good Morning!');
else
document.write('Good Afternoon!');
</script>
<p title=quot;Good Day Messagequot;>Good Morning!</p>
var now = new Date();
if(now.getHours() >= 12)
{
var goodDay = $('p[title=quot;Good Day Messagequot;]');
goodDay.text('Good Afternoon!');
}
Unobtrusive JavaScript: Never depend on JavaScript
<a href=quot;javascript:window.open('page.html')quot;>my page</a>
<a href=quot;#quot; onclick=quot;window.open('page.html')quot;>my page</a>
<a href=quot;page.htmlquot; onclick=quot;window.open(this.href)quot;>my page</a>
<a href=quot;page.htmlquot; class=quot;popupquot;>my page</a>
//some java script to open element with class quot;.popupquot; in a new window