3
\$\begingroup\$

I have the following type of many functions so many if condtions are there is there is any way to reduce no. of lines.

enter: function(request, callback, callback1){
 request.on={};
 if(callback==undefined && NodeClientUI.initdata.join!=undefined){
 callback=NodeClientUI.initdata.join;
 request.on.join=function(data){
 callback(data);
 };
 }else if(callback!=undefined ){
 request.on.join=function(data){
 callback(data);
 };
 }
 if(callback1==undefined && NodeClientUI.initdata.occupant!=undefined){
 callback1=NodeClientUI.initdata.occupant;
 request.on.occupant=function(data){
 callback1(data);
 };
 }else if(callback1!=undefined ){
 request.on.occupant=function(data){
 callback1(data);
 };
 }
 };

No of paramerter the callbacks may be increased in many functions.

Please suggest .

asked Jul 14, 2011 at 8:00
\$\endgroup\$
2
  • \$\begingroup\$ Is there no way to receive an array of callbacks? Or perhaps make some sort of resizing array for callbacks prior to the calling of this method? It would allow you to reduce this quite a bit if you could generalize. Though without further information, it's difficult to say how this method would "grow" with increasing callbacks (NodeClientUI.initdata contains what?). \$\endgroup\$ Commented Jul 14, 2011 at 10:03
  • \$\begingroup\$ okay i got your point of converting callbacks to array Please update your answer according. i can give the key of array join for callback and occupant for callback1 respectively \$\endgroup\$ Commented Jul 14, 2011 at 11:00

1 Answer 1

3
\$\begingroup\$

Try this:

enter: function(request, callbacks){
 request.on={};
 for(var key in callbacks) {
 var callback = callbacks[key];
 if(callback === undefined) {
 callback = NodeClientUI.initData[key];
 }
 request.on[key]=function(data){
 callback(data);
 }
 }
 };

This permits you to call with callbacks defined as:

var callbacks = {};
callbacks['join'] = function(data) { // join logic ... };
callbacks['occupant'] = function(data) { // occupant logic ... };
callbacks['stuff'] = undefined; // Takes on value of NodeClientUI.initData.stuff
...

Request would get assigned in the same manner. If you can't pass a map of callbacks in this way, you could create a function which creates a global variable callbacks for you to use inside the method (just ignore the callback parameter passed to enter method and use callbacks instead):

var callbacks = {};
function addCallback(key, callback) {
 callbacks[key] = callback;
}
answered Jul 14, 2011 at 13:23
\$\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.