1
\$\begingroup\$

How can I re-factor the code to remove duplication and create a common method ?

(function(){
 $("#a", "#main").bind("mouseover", function(){
 var id1 = $("#one").text(),
 args = ["DCSext.common1","common1","DCSext.common2","DCSext.title","one", "DCSext.ti", id1];
 dcsMultitrack.apply(this, args);
 });
 $("#b", "#cool").bind("click", function(){
 var id2 = $("#two").text(),
 args = ["DCSext.common1","common1","DCSext.common2","DCSext.title", "two", "DCSext.some", id2];
 dcsMultitrack.apply(this, args);
 });
 $("body").delegate("a", "click", function(){
 var id3 = $("#three").text(),
 args = ["DCSext.common1","common1","DCSext.common2","DCSext.new", "what", "DCSext.where", "us"];
 dcsMultitrack.apply(this, args);
 });
 }());

I have some common logs which are almost repeated in all callbacks. I can use a variable like

var commonlogs = ["DCSext.common1","common1","DCSext.common2","common2", "DCSext.common3", "common3" ];

i can use commonlogs.push("DCSext.title","one","DCSext.ti", "two"). But not finding a proper way to re-factoring repeating the DCSext stuff again and again since its very granular level .

Thanks for any advice or suggestions.

konijn
34.2k5 gold badges70 silver badges267 bronze badges
asked Jan 5, 2012 at 12:57
\$\endgroup\$
2
  • \$\begingroup\$ @paul Can you clarify your problem? I don't understand what you mean by "common logs". Are you referring to the args array you're passing into dcsMultitrack()? The commonlogs array you posted includes some strings that aren't in any of the above args arrays. \$\endgroup\$ Commented Jan 17, 2012 at 2:05
  • \$\begingroup\$ @seand purpose of including commonlogs variable is to move out repeated strings from the existing arrays in callback function .If you see my top code mostly ["DCSext.common1","common1","DCSext.common2"] been repeated almost in all callback function .So I can extract into an variable and can just do commonlogs.push(extra strings) but i do not think it i san elegent solution .So looking for some other advice. Thanks \$\endgroup\$ Commented Feb 7, 2012 at 6:43

1 Answer 1

2
\$\begingroup\$

There is not much you can do, the only thing I would suggest is to use concat instead of push, this way you can keep re-using commonLogs, and maybe have 1 commonLogs per group.

So

 var commonLogs = [ [] ];
 commonLogs[1] = ["DCSext.common1","common1"];
 commonLogs[2] = commonLogs[1].concat( ["DCSext.common2","common2"] );
 commonLogs[3] = commonLogs[2].concat( ["DCSext.common3","common3"] );

Then you can

(function(){
 $("#a", "#main").bind("mouseover", function(){
 var id1 = $("#one").text(),
 args = commonLogs[2].concat( ["DCSext.title","one", "DCSext.ti", id1] );
 dcsMultitrack.apply(this, args);
 });
 $("#b", "#cool").bind("click", function(){
 var id2 = $("#two").text(),
 args = commonLogs[2].concat( ["DCSext.title", "two", "DCSext.some", id2] );
 dcsMultitrack.apply(this, args);
 });
 $("body").delegate("a", "click", function(){
 var args = commonLogs[2].concat( ["DCSext.new", "what", "DCSext.where", "us"] );
 dcsMultitrack.apply(this, args);
 });
}());
answered Jan 28, 2014 at 3:01
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.