Skip to main content
Code Review

Return to Answer

Bounty Awarded with 100 reputation awarded by Community Bot
4, 5, 6 added
Source Link
tomalec
  • 352
  • 4
  • 8

Here you have some general thoughts:

  1. First of all, cache jQuery elements (var $this = $(this); instead of multiple executions of $(this)). http://jsfiddle.net/tomalec/pXPdS/1/

  2. You can also chain jQuery methods, it simplifies codes, and helps with above.

  3. To reduce amount of code you can use toggleClass instead of addClass removeClass.http://jsfiddle.net/tomalec/pXPdS/2/

  4. Instead of selecting $('#rankDrag') in start callback you can use reference given in arguments ui.helperhttp://jsfiddle.net/tomalec/pXPdS/3/

  5. To get id of empty/not empty holder ids, in my opinion, .map is more readable than .each. Moreover, you can move that function outside rearrange to avoid re-declaring it every time.

     function getTruncatedId(index, element){
     return element.id.substring(14);
     }
     function rearrange(dropid) {
     //..
     emptyHolders = holders.filter('.holder-empty').map(getTruncatedId);
     isRanked = holders.filter(':not(.holder-empty)').map(getTruncatedId);
     //..
     }
    

http://jsfiddle.net/tomalec/pXPdS/4/

  1. Pass dropped element to rearrange, so you will not have to traverse through DOM again:

     rearrange( $(this).attr('id') );//dropId = $(this).attr('id');
     //..
     isEmpty = $('#'+dropId).hasClass('holder-empty');
    

http://jsfiddle.net/tomalec/pXPdS/5/

Here you have some general thoughts:

  1. First of all, cache jQuery elements (var $this = $(this); instead of multiple executions of $(this)). http://jsfiddle.net/tomalec/pXPdS/1/

  2. You can also chain jQuery methods, it simplifies codes, and helps with above.

  3. To reduce amount of code you can use toggleClass instead of addClass removeClass.http://jsfiddle.net/tomalec/pXPdS/2/

Here you have some general thoughts:

  1. First of all, cache jQuery elements (var $this = $(this); instead of multiple executions of $(this)). http://jsfiddle.net/tomalec/pXPdS/1/

  2. You can also chain jQuery methods, it simplifies codes, and helps with above.

  3. To reduce amount of code you can use toggleClass instead of addClass removeClass.http://jsfiddle.net/tomalec/pXPdS/2/

  4. Instead of selecting $('#rankDrag') in start callback you can use reference given in arguments ui.helperhttp://jsfiddle.net/tomalec/pXPdS/3/

  5. To get id of empty/not empty holder ids, in my opinion, .map is more readable than .each. Moreover, you can move that function outside rearrange to avoid re-declaring it every time.

     function getTruncatedId(index, element){
     return element.id.substring(14);
     }
     function rearrange(dropid) {
     //..
     emptyHolders = holders.filter('.holder-empty').map(getTruncatedId);
     isRanked = holders.filter(':not(.holder-empty)').map(getTruncatedId);
     //..
     }
    

http://jsfiddle.net/tomalec/pXPdS/4/

  1. Pass dropped element to rearrange, so you will not have to traverse through DOM again:

     rearrange( $(this).attr('id') );//dropId = $(this).attr('id');
     //..
     isEmpty = $('#'+dropId).hasClass('holder-empty');
    

http://jsfiddle.net/tomalec/pXPdS/5/

Source Link
tomalec
  • 352
  • 4
  • 8

Here you have some general thoughts:

  1. First of all, cache jQuery elements (var $this = $(this); instead of multiple executions of $(this)). http://jsfiddle.net/tomalec/pXPdS/1/

  2. You can also chain jQuery methods, it simplifies codes, and helps with above.

  3. To reduce amount of code you can use toggleClass instead of addClass removeClass.http://jsfiddle.net/tomalec/pXPdS/2/

default

AltStyle によって変換されたページ (->オリジナル) /