javascript - jQuery Refactoring - DRY -


i have these 2 functions, inside of main function. you'll see difference between 2 of them in middle how append/edit html. thinking nice come 2 new functions, 1 first half , other second half. i'm not sure if possible jquery, or javascript matter, don't know how call these new functions inside of these functions, if makes sense. help/guidance great!

here first one

$('#save').click(function(){ var title = $('#title').val(); var tags = $('#tags').val(); var notes = $('#notes').val(); var mydate = new date(); if (title.length < 1) {   $('.title-warn').show(); } if (tags.length < 1) {   $('.tags-warn').show(); } if (notes.length < 1) {   $('.notes-warn').show(); } if (title.length >= 1 && tags.length >= 1 && notes.length >= 1) {   $allnotes.prepend('<li class="note"><div><h1>' + title + '</h1><div class="date">      <h2>'+ mydate.todatestring() +'</h2><span class="btn btn-edit">edit</span></div><h3>' + tags + '</h3><p>' + notes + '</p></div></li>');   $allnotes.show();   $newnote.hide();   $('.title-warn').hide();   $('.tags-warn').hide();   $('.notes-warn').hide(); } $('#title').val(''); $('#tags').val(''); $('#notes').val(''); $('#search').prop('disabled', false); $('#search').attr("placeholder", "search title, tags, date, or words/sentences in notes"); $('.btn-search').prop('disabled', false); }); 

and second one

$('#edit').click(function(){ var title = $('#edit-title').val(); var tags = $('#edit-tags').val(); var notes = $('#edit-notes').val(); var mydate = new date(); if (title.length < 1) {   $('.title-warn').show(); } if (tags.length < 1) {   $('.tags-warn').show(); } if (notes.length < 1) {   $('.notes-warn').show(); } if (title.length >= 1 && tags.length >= 1 && notes.length >= 1) {   $('.edited-note').html('<div><h1>' + title + '</h1><div class="date">      <h2>'+ mydate.todatestring() +'</h2><span class="btn btn-edit">edit</span></div><h3>' + tags + '</h3><p>' + notes + '</p></div>');   $('.allnotes').show();   $('.edit-note').hide();   $('.title-warn').hide();   $('.tags-warn').hide();   $('.notes-warn').hide(); } $('#title').val(''); $('#tags').val(''); $('#notes').val(''); $('#search').prop('disabled', false); $('#search').attr("placeholder", "search title, tags, date, or words/sentences in notes"); $('.btn-search').prop('disabled', false); $('.edited-note').removeclass('edited-note'); }); 

and of course if has suggestions on other aspects of code criticism!

you answered own question! "as you'll see difference between 2 of them in middle how append/edit html." initial pretty naive , mechanical attempt attempt @ refactoring might this, pulling out common code shared functions:

function prehandler(title, tags, notes) {     if (title.length < 1) {         $('.title-warn').show();     }     if (tags.length < 1) {         $('.tags-warn').show();     }     if (notes.length < 1) {         $('.notes-warn').show();     }      return title.length >= 1 && tags.length >= 1 && notes.length >= 1;  }   function commonpost () {      $('#title').val('');     $('#tags').val('');     $('#notes').val('');     $('#search').prop('disabled', false);     $('#search').attr("placeholder", "search title, tags, date, or words/sentences in notes");     $('.btn-search').prop('disabled', false);   }    $('#save').click(function(){     var title = $('#title').val();     var tags = $('#tags').val();     var notes = $('#notes').val();     var mydate = new date();     if  (prehandler(title, tags, notes)) {          $allnotes.prepend('<li class="note"><div><h1>' + title + '</h1><div class="date">      <h2>'+ mydate.todatestring() +'</h2><span class="btn btn-edit">edit</span></div><h3>' + tags + '</h3><p>' + notes + '</p></div></li>');         $allnotes.show();         $newnote.hide();         $('.title-warn').hide();         $('.tags-warn').hide();         $('.notes-warn').hide();      }     commonpost();  });   $('#edit').click(function() {     var title = $('#edit-title').val();     var tags = $('#edit-tags').val();     var notes = $('#edit-notes').val();     var mydate = new date();      if  (prehandler(title, tags, notes)) {          $('.edited-note').html('<div><h1>' + title + '</h1><div class="date">      <h2>'+ mydate.todatestring() +'</h2><span class="btn btn-edit">edit</span></div><h3>' + tags + '</h3><p>' + notes + '</p></div>');         $('.allnotes').show();         $('.edit-note').hide();         $('.title-warn').hide();         $('.tags-warn').hide();         $('.notes-warn').hide();      }      commonpost();     $('.edited-note').removeclass('edited-note');  }); 

it doesn't win when there 2 scenarios. more 2 doing sort of refactoring starts reap rewards.

this first attempt refined (a lot). perhaps nice folks post. first attempt.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -