Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Modularização de código JS
Search
Rafael Macedo
October 18, 2014
Programming
250
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Modularização de código JS
Rafael Macedo
October 18, 2014
More Decks by Rafael Macedo
See All by Rafael Macedo
TDC 2014 - Solucionando o problema de Uploads em Apps no Heroku
macedorafael
1
170
GuruSP - Solucionando o problema de Uploads em Apps no Heroku
macedorafael
2
120
Aplicações Realtime com XMPP
macedorafael
3
220
Web in the cloud with ruby
macedorafael
2
400
Other Decks in Programming
See All in Programming
Vite+ Unified Toolchain for the Web
naokihaba
0
370
dRuby over BLE
makicamel
2
390
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
14
6.5k
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.5k
Lessons from Spec-Driven Development
simas
PRO
0
230
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
220
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.6k
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
200
Inside Stream API
skrb
1
810
A2UI という光を覗いてみる
satohjohn
1
160
The NotImplementedError Problem in Ruby
koic
1
980
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
130
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Six Lessons from altMBA
skipperchong
29
4.3k
Abbi's Birthday
coloredviolet
3
8.3k
Building an army of robots
kneath
306
46k
Rails Girls Zürich Keynote
gr2m
96
14k
From π to Pie charts
rasagy
0
220
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
How to make the Groovebox
asonas
2
2.2k
4 Signs Your Business is Dying
shpigford
187
22k
The Cost Of JavaScript in 2023
addyosmani
55
10k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
Transcript
MODULARIZAÇÃO DE CÓDIGO JS @macedorafael
RAFAEL MACEDO github.com/macedo twitter.com/macedorafael
None
None
None
None
CODE MINER
CODE MINER
[email protected]
None
http://github.com
6 dos 10 projetos mais populares
560k repositórios criados em 2014* *até o momento 10/10/2014
JS deixou de ser a segunda linguagem a algum tempo
“JS may look familiar…
… but it really isn’t. Take your time to get
to know it.” @goloroden
None
application.js app/assets/javascripts/
//= require modernizr //= require jquery //= require jquery.validate //=
require jquery.validate.additional-methods //= require jquery_ujs //= require fancybox //= require_self //= require jquery.ui.datepicker //= require jquery.ui.draggable //= require underscore //= require jquery.tokeninput //= require application/browser_selector //= require application/jQuery_gtSlider //= require application/jQuery_gtSelect //= require application/bxslider //= require application/jquery.form //= require application/validations //= require jquery_nested_form //= require ckeditor/init //= require ckeditor/config //= require application/jquery.nicescroll //= require custome_js/user_journey //= require application/album_images //= require application/home //= require application/user //= require jquery.rating //= require jquery.imagedrag.min //= require slider/jssor.core //= require slider/jssor.slider //= require slider/jssor.utils //= require scroll_paginate //= require destination //= require magicselection //= require jquery.quicksearch //= require jquery.blockUI $(document).on('click','.star',function(){ $(this).parents('form:first').submit(); }); $(document).ready(function() { $('#rateform :radio.star').rating({cancel: 'Cancel', cancelValue: '0'}); $("a.fancybox").fancybox({ 'transitionIn' : 'none', 'transitionOut' : 'none', 'autoScale' : false, 'type' : 'iframe',
allowfullscreen : 'true', allowscriptaccess : 'always', closeBtn : false }).trigger('click');
$("#from_date").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true }); $("#to_date").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true }); }); $(document).on('click', '.bt-deletar', function(){ $(this).parents('li').remove(); }); // Hover box editar perfil function hover_perfil_box() { if ($('.lista-lugares').size() > 0) { $(".lista-lugares li").on('mouseover',function (e){ e.preventDefault(); $(this).children(".details").stop().animate({ "top": "94px" }, "fast" ); }); $(".lista-lugares li").on('mouseleave',function (e){ e.preventDefault(); $(this).children(".details").stop().animate({ "top": "120px" }, "fast" ); }); } } $(document).on('click', '.unblock_user', function(){ $(this).parents('li').addClass('none'); }); $(document).on('click','a.circle, div.msg-content-box',function(){ $(this).parents('div.msg-content-box').removeClass('read'); $(this).removeClass('read'); }); $(document).on('click','a.folder, a.del',function(){ id = $(this).parents('div.msg-content-box').prop('id') $("div[id="+id+"]").fadeOut("slow") $.fancybox.close();
allowfullscreen : 'true', allowscriptaccess : 'always', closeBtn : false }).trigger('click');
$("#from_date").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true }); $("#to_date").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true }); }); $(document).on('click', '.bt-deletar', function(){ $(this).parents('li').remove(); }); // Hover box editar perfil function hover_perfil_box() { if ($('.lista-lugares').size() > 0) { $(".lista-lugares li").on('mouseover',function (e){ e.preventDefault(); $(this).children(".details").stop().animate({ "top": "94px" }, "fast" ); }); $(".lista-lugares li").on('mouseleave',function (e){ e.preventDefault(); $(this).children(".details").stop().animate({ "top": "120px" }, "fast" ); }); } } $(document).on('click', '.unblock_user', function(){ $(this).parents('li').addClass('none'); }); $(document).on('click','a.circle, div.msg-content-box',function(){ $(this).parents('div.msg-content-box').removeClass('read'); $(this).removeClass('read'); }); $(document).on('click','a.folder, a.del',function(){ id = $(this).parents('div.msg-content-box').prop('id') $("div[id="+id+"]").fadeOut("slow") $.fancybox.close(); 1500 LOC
allowfullscreen : 'true', allowscriptaccess : 'always', closeBtn : false }).trigger('click');
$("#from_date").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true }); $("#to_date").datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true }); }); $(document).on('click', '.bt-deletar', function(){ $(this).parents('li').remove(); }); // Hover box editar perfil function hover_perfil_box() { if ($('.lista-lugares').size() > 0) { $(".lista-lugares li").on('mouseover',function (e){ e.preventDefault(); $(this).children(".details").stop().animate({ "top": "94px" }, "fast" ); }); $(".lista-lugares li").on('mouseleave',function (e){ e.preventDefault(); $(this).children(".details").stop().animate({ "top": "120px" }, "fast" ); }); } } $(document).on('click', '.unblock_user', function(){ $(this).parents('li').addClass('none'); }); $(document).on('click','a.circle, div.msg-content-box',function(){ $(this).parents('div.msg-content-box').removeClass('read'); $(this).removeClass('read'); }); $(document).on('click','a.folder, a.del',function(){ id = $(this).parents('div.msg-content-box').prop('id') $("div[id="+id+"]").fadeOut("slow") $.fancybox.close(); 1500 LOC
None
None
None
3k
15k 3k
¯\_(ツ)_/¯
Modularização
• Redução de complexidade • Encapsulamento • Manutenabilidade ???
None
None
None
None
None
None
None
None
None
http://todomvc.com http://todomvc.com
MVC
Model Gerencimento de dados, persistência
View UX, renderiza templates com os dados do modelo, eventos
Controller Gerencia eventos entre models e views
None
None
http://bit.ly/1de5qfT
module revealing module mediator prototype …
module pattern
(function() { // your code here })();
var Module = (function() { // your code here })();
var Module = (function() { var privateAttribute = '';
var privateMethod = function() { /* ... */ }; })();
var Module = (function() { var privateAttribute = '';
var privateMethod = function() { /* ... */ }; })();
var Module = (function() { var privateAttribute = '';
var privateMethod = function() { /* ... */ }; return { publicMethod: function() { /* ... */ } } })();
var Module = (function() { var privateAttribute = '';
var privateMethod = function() { /* ... */ }; return { publicMethod: function() { /* ... */ } } })();
var Module = (function() { var privateAttribute = '';
var privateMethod = function() { /* ... */ }; return { publicMethod: function() { /* ... */ } } })();
var Module = (function() { var privateAttribute = '';
var privateMethod = function() { /* ... */ }; return { publicMethod: function() { /* ... */ } } })();
var QuestionsList = (function() { var collection = [1, 2,
3, 4, 5] , currentIndex = 0; return { current: function() { var i = currentIndex % collection.length; return 'Question: ' + collection[i]; }, next: function() { currentIndex += 1; }, prev: function() { currentIndex -= 1; } } })();
var QuestionsList = (function() { var collection = [1, 2,
3, 4, 5] , currentIndex = 0; return { current: function() { var i = currentIndex % collection.length; return 'Question: ' + collection[i]; }, next: function() { currentIndex += 1; }, prev: function() { currentIndex -= 1; } } })();
var QuestionsList = (function() { var collection = [1, 2,
3, 4, 5] , currentIndex = 0; return { current: function() { var i = currentIndex % collection.length; return 'Question: ' + collection[i]; }, next: function() { currentIndex += 1; }, prev: function() { currentIndex -= 1; } } })();
None
revealing module pattern
var QuestionsList = (function() { var collection = [1, 2,
3, 4, 5] , currentIndex = 0; function current() { var i = currentIndex % collection.length; return 'Question: ' + collection[i]; } function next() { currentIndex += 1; return current(); } function prev() { currentIndex -= 1; } return { current: current, next: next, prev: prev } })();
var QuestionsList = (function() { var collection = [1, 2,
3, 4, 5] , currentIndex = 0; function current() { var i = currentIndex % collection.length; return 'Question: ' + collection[i]; } function next() { currentIndex += 1; return current(); } function prev() { currentIndex -= 1; } return { current: current, next: next, prev: prev } })();
var QuestionsList = (function() { var collection = [1, 2,
3, 4, 5] , currentIndex = 0; function current() { var i = currentIndex % collection.length; return 'Question: ' + collection[i]; } function next() { currentIndex += 1; return current(); } function prev() { currentIndex -= 1; } return { current: current, next: next, prev: prev } })();
var QuestionsList = (function() { var collection = [1, 2,
3, 4, 5] , currentIndex = 0; function current() { var i = currentIndex % collection.length; return 'Question: ' + collection[i]; } function next() { currentIndex += 1; return current(); } function prev() { currentIndex -= 1; } return { current: current, next: next, prev: prev } })();
var QuestionsList = (function() { ... function _getIndex()
{ return currentIndex % collection.length; } function _incrementIndex() { currentIndex += 1; } function current() { return 'Question: ' + collection[_getIndex()]; } function next() { _incrementIndex(); return current(); } return { current: current, next: next } })();
var QuestionsList = (function() { ... function _getIndex()
{ return currentIndex % collection.length; } function _incrementIndex() { currentIndex += 1; } function current() { return 'Question: ' + collection[_getIndex()]; } function next() { _incrementIndex(); return current(); } return { current: current, next: next } })();
var QuestionsList = (function() { ... function _getIndex()
{ return currentIndex % collection.length; } function _incrementIndex() { currentIndex += 1; } function current() { return 'Question: ' + collection[_getIndex()]; } function next() { _incrementIndex(); return current(); } return { current: current, next: next } })();
Object { current: function, next: function } QuestionsList
Object { current: function, next: function, random: function } ExtraQuestionsList
var ExtraQuestionsList = (function(QuestionsList) { QuestionsList.randomQuestion() { /* ...
*/ } return QuestionList; })(QuestionsList || {});
pub/sub pattern
None
None
None
var Mediator = { obj: $({}), publish: function(event, data) {
return this.obj.trigger(event, data); }, subscribe: function(event, fn) { return this.obj.on(event, fn); }, unsubscribe: function(event, fn) { return this.obj.off(event, fn); } };
var QuestionViewer = (function(){ function onChange() { alert('current question
changed!'); // fetch new question // render ... } return { init: function() { Mediator.subscribe( 'change:question', onChange ); } } })(); QuestionViewer.init();
var QuestionsList = (function() { … function next() {
currentIndex += 1; Mediator.publish( 'change:question', { index: currentIndex } ); return current(); } … })();
AMD (Asyncronous Module Definition)
define( [ 'dep1', 'dep2' ], function(A, B) { /*
...... */ } );
define( [ "js/jquery.js", "js/jquery.color.js", "js/underscore.js" ], function($, colorPlugin, _){
var shuffleColor = _.first( _.shuffle([“#666", "#333", "#111"]) ); $( ".item" ).animate({ "backgroundColor": shuffleColor }); return {}; } );
define( [ "js/jquery.js", "js/jquery.color.js", "js/underscore.js" ], function($, colorPlugin, _){
var shuffleColor = _.first( _.shuffle([“#666", "#333", "#111"]) ); $( ".item" ).animate({ "backgroundColor": shuffleColor }); return {}; } );
define( [ "js/jquery.js", "js/jquery.color.js", "js/underscore.js" ], function($, colorPlugin, _){
var shuffleColor = _.first( _.shuffle([“#666", "#333", "#111"]) ); $( ".item" ).animate({ "backgroundColor": shuffleColor }); return {}; } );
Organização
#1 Separe módulos por arquivos
#2 Concatene e minifique os arquivos para deploy (Grunt, Gulp)
#3 Code Style (JSHint, JSLint)
#4 Escreva testes (Jasmine, Mocha)
OBRIGADO ! @macedorafael speakerdeck.com/macedorafael