JavaScript and UI Architecture Best Practices
Upcoming SlideShare
Loading in...5
×
 

JavaScript and UI Architecture Best Practices

on

  • 16,294 views

Day 5 of 7-days "JavaScript and Rich User Interfaces" training for my colleagues. It covers coding and design patterns.

Day 5 of 7-days "JavaScript and Rich User Interfaces" training for my colleagues. It covers coding and design patterns.

Statistics

Views

Total Views
16,294
Views on SlideShare
15,408
Embed Views
886

Actions

Likes
14
Downloads
372
Comments
0

47 Embeds 886

http://mrthangaraj.blogspot.com 383
http://mrthangaraj.blogspot.in 137
http://www.slideshare.net 43
http://mrthangaraj.blogspot.com.au 41
http://mrthangaraj.blogspot.co.uk 34
http://paper.li 34
http://mrthangaraj.blogspot.ca 24
http://www.scoop.it 15
http://mrthangaraj.blogspot.com.br 14
http://mrthangaraj.blogspot.hu 9
http://wiki.skygate.pl 9
http://mrthangaraj.blogspot.hk 9
http://mrthangaraj.blogspot.co.nz 9
http://mrthangaraj.blogspot.nl 9
http://mrthangaraj.blogspot.fr 9
http://mrthangaraj.blogspot.sg 8
http://www.linkedin.com 8
http://mrthangaraj.blogspot.mx 7
http://mrthangaraj.blogspot.de 7
http://mrthangaraj.blogspot.com.ar 6
https://groupereflect.bluekiwi.net 6
http://mrthangaraj.blogspot.ro 5
http://mrthangaraj.blogspot.ru 5
http://mrthangaraj.blogspot.it 5
http://mrthangaraj.blogspot.jp 5
http://mrthangaraj.blogspot.dk 4
http://mrthangaraj.blogspot.com.tr 4
http://mrthangaraj.blogspot.be 3
http://translate.googleusercontent.com 3
http://mrthangaraj.blogspot.kr 3
http://mrthangaraj.blogspot.co.il 3
http://mrthangaraj.blogspot.gr 3
http://mrthangaraj.blogspot.no 2
http://mrthangaraj.blogspot.pt 2
http://mrthangaraj.blogspot.se 2
http://mrthangaraj.blogspot.ae 2
http://mrthangaraj.blogspot.ie 2
http://pinterest.com 2
http://mrthangaraj.blogspot.co.at 2
http://mrthangaraj.blogspot.com.es 1
http://mrthangaraj.blogspot.tw 1
https://twitter.com 1
http://www.ig.gmodules.com 1
http://static.slidesharecdn.com 1
http://mrthangaraj.blogspot.cz 1
http://mrthangaraj.blogspot.fi 1
http://mrthangaraj.blogspot.ch 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

JavaScript and UI Architecture Best Practices JavaScript and UI Architecture Best Practices Presentation 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. “
  • Namespaces: Defining namespace //defining top-level package var MyApp = MyApp || {}; //defining package //defining package MyApp.dom = { MyApp.string = MyApp.string || {}; addEventListener: function(element, //defining package callback) { MyApp.string.utils = { //code goes here trim: function(str) { }, //code goes here removeEventListener: function(element, }, callback) { reverse: function(str) { //code goes here //code goes here } } }; };
  • Namespaces: Usage in code MyApp.string.utils.reverse(...); MyApp.string.utils.trim(...); MyApp.dom.addEventListener(...); MyApp.dom.removeEventListener(...);
  • Namespaces: namespace() function //defining top-level package var MyApp = MyApp || {}; //defines package structure MyApp.namespace = function(name) { if(name) { //some checks if name is valid var names = name.split(quot;.quot;); var current = MyApp; for(var i in names) { if(!current[names[i]]) { current[names[i]] = {}; } current = current[names[i]]; } return true; } return false; };
  • Namespaces: Defining namespace //defining top-level package var MyApp = MyApp || {}; //defining package //defining package MyApp.string = MyApp.string || {}; MyApp.namespace(quot;string.utilsquot;); //defining package //defining package MyApp.string.utils = { MyApp.string.utils.trim = function(str) { trim: function(str) { //code goes here //code goes here console.log(quot;trimquot;); }, }; reverse: function(str) { //code goes here MyApp.string.utils.reverse = function(str) { } //code goes here }; console.log(quot;reversequot;); };
  • Questions?
  • Init-time branching
  • 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.”
  • Init-time branching: Defining browser-specific methods MyApp.namespace(quot;domquot;); MyApp.dom.addListener = null; //all major browsers if(typeof window.addEventListener === 'function') { MyApp.dom.addListener = function(el, type, fn) { el.addEventListener(type, fn, false); }; } //IE else if(typeof document.attachEvent === 'function') { MyApp.dom.addListener = function(el, type, fn) { el.attachEvent('on' + type, fn); }; } //older browsers else { MyApp.dom.addListener = function(el, type, fn) { el['on' + type] = fn; }; }
  • Questions?
  • Lazy definition
  • 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.”
  • Lazy definition: Defining browser-specific methods var MyApp = MyApp || {}; Override function first time MyApp.dom = { addListener: function(el, type, fn){ if (typeof el.addEventListener === 'function') { MyApp.dom.addListener = function(el, type, fn) { el.addEventListener(type, fn, false); }; } else if (typeof el.attachEvent === 'function'){ MyApp.dom.addListener = function(el, type, fn) { el.attachEvent('on' + type, fn); }; } else { MyApp.dom.addListener = function(el, type, fn) { el['on' + type] = fn; }; } MyApp.dom.addListener(el, type, fn); } };
  • Questions?
  • Configuration object
  • 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: Ordinary constructor var MyApp = MyApp || {}; MyApp.namespace(quot;domquot;); MyApp.dom.Button = function(text, type) { //some code here } MyApp.dom.Button = function(text, type, color, border, font) { //some code here }
  • 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.”
  • Self-executing functions: Usage (function() { //code goes here })(); (function(){ // ... var jQuery = window.jQuery = window.$ = function( selector, context ) { // ... return new jQuery.fn.init( selector, context ); }; // ... jQuery.fn = jQuery.prototype = { init: function() { //... } }; })();
  • Questions?
  • Chaining
  • 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
  • Questions?