1

Alright lets see if I can describe this.

I have a handful of functionality that was created sometime ago, and works swimmingly. However there is a new desired spec, so without having to rewrite the code base in a matter of speaking, and without having to double up on code to pull the same effect off I am trying to figure out how I can go about making something jump back higher in the code within the same function to repeat the run of the function rather then doing the same code again below.

What I have is a click based triggers ie:

$('.selector').click(function(){});

In this function is about 30 lines of functionality to create a new element and populate it accordingly. However unfortunately in that same bit of functionality there is conditions to wether it should or not.

*The previous requirement was when the element it creates is open and populated just throw an alert() saying essentially wrap up what your doing, and then go on to the next.

*Now the new requirement is just close that and open a new element.
Which I've gotten to close out the existing, and do everything I want it to do, except the population of the new element which is above where the condition is currently.

Knowing there is no "go to" type of logic in javascript (or last I knew). the only thing I can think of is taking the same code from above and putting it in the condition as well, doubling up on the code and having litterally 2 copies of the same bit. I want to avoid that, but cant think of a way to do it. So here I am looking for ideas

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
chris
  • 36,115
  • 52
  • 143
  • 252
  • 5
    Sounds like yo need to refactor a bit. Its probably not as scary as you think? Maybe post some code. – Michael Christensen Feb 08 '13 at 18:00
  • Yea, refactoring is the unfortunate realistic route. Guess I was just hoping to find an easier way. Currently playing with some ideas to refactor. So chances are even if someone gave me a good alternative, Ill have something working by then. Though, an answer will give me something to factor into newer things down the road so I dont run into this hump again – chris Feb 08 '13 at 18:01
  • 1
    Well, unless you post some code examples of what you're doing, all i can say is: if you need "goto" functionallity, you're doing something wrong. – tobbr Feb 08 '13 at 18:05
  • 1
    Javascript does indeed have a `goto` but you should probably refactor instead of using it. Maybe put the repetitious code inside a function and call it twice? – Elliot Bonneville Feb 08 '13 at 18:06
  • @ElliotBonneville huh? Where are you getting that? http://stackoverflow.com/questions/9751207/how-can-i-use-goto-in-javascript – Ben McCormick Feb 08 '13 at 18:08
  • 1
    ...or even better , make some event driven pattern, you're using javascript after all, use its advantages – kidwon Feb 08 '13 at 18:08
  • 1
    Do you know that callbacks do not have to be anonymous functions and they can be named? – epascarello Feb 08 '13 at 18:10
  • @ben336: http://stackoverflow.com/a/12368851/339852 – Elliot Bonneville Feb 08 '13 at 18:11
  • @ElliotBonneville huh, learned something new. Still seems like an awful idea in general :) But thanks for sharing! – Ben McCormick Feb 08 '13 at 18:18
  • @ben336: Yes it's an awful terrible idea and I'm very sad it exists in Javascript. :( – Elliot Bonneville Feb 08 '13 at 18:21

2 Answers2

4

Knowing there is no "go to" type of logic in javascript (or last I knew). the only thing I can think of is taking the same code from above and putting it in the condition as well, doubling up on the code and having litterally 2 copies of the same bit. I want to avoid that, but cant think of a way to do it. So here I am looking for ideas

Why don't you just pull this piece of code out into a function? You can run the function if the conditional is true in the original instance, and run it all the time in your callback? This is fairly minimal refactoring, just move the code out of the logic into a separate function, keeping it as is and maybe making some of the referenced variables into parameters.

So something like this if you want to run all the actions regardless of the conditional statements:

...
if(condition){
  actionA();
}
if(condition2){
  actionB();
}
...

$('.selector').click(function(){
    actionA();
    actionB();
});    
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
1

You're familiar with that pattern, right?

var aCallback =  function(){........};
$('.selector').click(aCallback);
kidwon
  • 4,448
  • 5
  • 28
  • 45