Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modularização de código JS

Modularização de código JS

Rafael Macedo

October 18, 2014
Tweet

More Decks by Rafael Macedo

Other Decks in Programming

Transcript

  1. //= 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',

  2. 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();

  3. 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
  4. 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
  5. 3k

  6. MVC

  7. var Module = (function() {
 
 var privateAttribute = '';


    
 var privateMethod = function() {
 /* ... */
 };
 })();
  8. var Module = (function() {
 
 var privateAttribute = '';


    
 var privateMethod = function() {
 /* ... */
 };
 })();
  9. var Module = (function() {
 
 var privateAttribute = '';


    
 var privateMethod = function() {
 /* ... */
 };
 
 return {
 publicMethod: function() {
 /* ... */
 }
 }
 })();
  10. var Module = (function() {
 
 var privateAttribute = '';


    
 var privateMethod = function() {
 /* ... */
 };
 
 return {
 publicMethod: function() {
 /* ... */
 }
 }
 })();
  11. var Module = (function() {
 
 var privateAttribute = '';


    
 var privateMethod = function() {
 /* ... */
 };
 
 return {
 publicMethod: function() {
 /* ... */
 }
 }
 })();
  12. var Module = (function() {
 
 var privateAttribute = '';


    
 var privateMethod = function() {
 /* ... */
 };
 
 return {
 publicMethod: function() {
 /* ... */
 }
 }
 })();
  13. 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;
 }
 }
 })();
  14. 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;
 }
 }
 })();
  15. 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;
 }
 }
 })();
  16. 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
 }
 })();
  17. 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
 }
 })();
  18. 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
 }
 })();
  19. 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
 }
 })();
  20. 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
 }
 })();
  21. 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
 }
 })();
  22. 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
 }
 })();
  23. var ExtraQuestionsList = (function(QuestionsList) {
 
 QuestionsList.randomQuestion() {
 /* ...

    */ 
 }
 
 return QuestionList;
 
 })(QuestionsList || {});
  24. 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);
 }
 };
  25. var QuestionViewer = (function(){
 
 function onChange() {
 alert('current question

    changed!');
 // fetch new question
 // render ...
 }
 
 return {
 init: function() {
 Mediator.subscribe(
 'change:question', onChange
 );
 }
 }
 })();
 
 QuestionViewer.init();
  26. var QuestionsList = (function() {
 …
 
 function next() {


    currentIndex += 1;
 Mediator.publish( 'change:question', { index: currentIndex } );
 return current();
 }
 …
 
 })();
  27. 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 {};
 }
 );
  28. 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 {};
 }
 );
  29. 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 {};
 }
 );