This question is primarily focused on how to manage code as you are developing, making it highly adaptable etc. Let me explain through this example, and it will make more sense. 'I will add bounty, if I need to'.
Our server is strapped for memory, and we are pushing a lot of sorting work onto the client side with Javascript/Jquery, to alleviate those issues. Here is the fiddle if you would like to follow along. https://jsfiddle.net/ydc6ywuz/23/
The problem comes with this code.
var sortSubSite = $('.AccessSitesLinks.False');
var subArr = sortSubSite.map(function(_, o){
return {
t: $(o).text(),
h: $(o).attr('href'),
c: $(o).attr('class')
};
}).get();
sortSubSite.each(function(i, o) {
var classList = $(o).attr('class').split(/\s+/);
$('.ContainingBox.AccessSitesLinks.True.'+ classList[2]).append($(o));
$(o).wrap("<div class='ContainingBox2'></div>");
$(o).text(subArr[i].t);
$(o).attr('href', subArr[i].h);
$(o).attr('class', subArr[i].c + classList[2]);
});
Each Home
site will have the class AccessSitesLinks True [Num]
. So all of its subsites would have AccessSitesLinks False [Num]
. I understand that sortSubSite and SubSite are similar and could be combined to be more extensible, and that's part of the question.
A new business requirement is to be able to sort the SubSites
alphabetically. And this is where the question comes into play. How do I take this existing code, and refactor it to continually be meeting business requirements? I can make the code sort much like I did with Home Sites
, but, this doesn't seem extensible, just creating another function to be called right after. I have MOST of the variables, arrays, and functions to sort alphabetically already. Is there something I am missing from a design perspective? Is it just inexperience that is making it so I can't see how to design this properly?
EDIT
I want to clarify the question more. Yes, I am obtaining the object $(o)
multiple times, and that can be inefficient. This question is more about how to redesign while coding continues as to not continually be piece-mealing code.
$(o).text('+').attr({ 'href': arr[i].h, 'class': arr[i].c })
instead for example$(o).etc.etc
can be more efficient Thanks again, this question is more about design, around reusing functions/vars/arrays, in an ever changing requirements. And do address the flatness, the server is done resource wise. This is what will be delivered, and will be sorted/ built from here.