Skip to content

Instantly share code, notes, and snippets.

@tusharmath
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tusharmath/9910975 to your computer and use it in GitHub Desktop.
Save tusharmath/9910975 to your computer and use it in GitHub Desktop.
What is the best approach to setup this view architecture
socket.on('message', function(data) {
//ListView needs to be updated based on the data and the current selected tab
if (user_current_tab === 'special_tab_1') {
take_action_for_special_tab_1(data);
} else if (user_current_tab === 'special_tab_2') {
take_action_for_special_tab_2(data);
} else {
//For all other 13 cases
take_default_action_for_other_tabs(data);
}
//Notification view needs to be updated irrespective of the tab selected.
take_action_notification_view(data);
});
@tusharmath
Copy link
Author

One possible problem that I can think of is - what happens when you have another special tab - special_tab_3, which shares a part of the logic with special_tab_1 . One could update the code as follows -

socket.on('message', function(data) {

    var take_action_for_special_tab_3 = function(data) {

        var options = {
            execute_as: 'special_tab_3'
        };

        //Calls the tab1 specific logic by telling him to execute it for tab3
        take_action_for_special_tab_1(data, options);
        /*
            Tab3 specific logic.
        */
    };

    if (user_current_tab === 'special_tab_1') {

        take_action_for_special_tab_1(data);

    } else if (user_current_tab === 'special_tab_2') {

        take_action_for_special_tab_2(data);

    } else if (user_current_tab === 'special_tab_3') {

        take_action_for_special_tab_3(data);

    } else {

        take_default_action_for_other_tabs(data);
    }

    take_action_notification_view(data);

});

Calling the take_action_for_special_tab_1 and telling it to execute as if we were on tab3 is just awful. Also, doesn't the if else block seem redundant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment