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
240
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
柔軟なPDFレイアウトエディタを支える型システム設計 — Discriminated UnionとConditional Typeの実践
minako__ph
4
1.5k
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
240
CLIであることを活かしたGitHub Copilot CLI活用術 / GitHub Copilot CLI Pro Tips & Tricks
nao_mk2
1
1.2k
GitHub Copilot CLIのいいところ
htkym
2
1.3k
開発体験を左右するライブラリの API 設計 - GraphQL スキーマ構築ライブラリから考える #tskaigi
izumin5210
2
1.6k
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
160
Oxcを導入して開発体験が向上した話
yug1224
4
290
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
150
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
600
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
110
The NotImplementedError Problem in Ruby
koic
1
630
Featured
See All Featured
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
530
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.3k
Fireside Chat
paigeccino
42
3.9k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
360
Writing Fast Ruby
sferik
630
63k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
250
Building an army of robots
kneath
306
46k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
550
Embracing the Ebb and Flow
colly
88
5.1k
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