vendredi 26 décembre 2014

javascript - dry - nested if statement

I am new to coding, but have been reading about DRY - Don't Repeat Yourself.


I have a JavaScript if/else statement that does not fit the DRY approach, but I am unable to workout how to write the JavaScript if/else statement so that the contents are not repeated.


I am hoping some one smarter than me can show me.


Here is my code:



{% if user.get_profile.subscription_category == '00' %}

$('#id_name_details_prefix_title').addClass('kmw-disabled');

$('#id_name_details_first_name').addClass('kmw-disabled');

$('#id_name_details_middle_name').addClass('kmw-disabled');

$('#id_name_details_last_name').addClass('kmw-disabled');

$('#id_name_details_suffix_title').addClass('kmw-disabled');

{% else %}

{% if user.get_profile.display_virtual_keyboard %}

$('#id_name_details_prefix_title').removeClass('kmw-disabled');

$('#id_name_details_first_name').removeClass('kmw-disabled');

$('#id_name_details_middle_name').removeClass('kmw-disabled');

$('#id_name_details_last_name').removeClass('kmw-disabled');

$('#id_name_details_suffix_title').removeClass('kmw-disabled');

{% else %}

$('#id_name_details_prefix_title').addClass('kmw-disabled');

$('#id_name_details_first_name').addClass('kmw-disabled');

$('#id_name_details_middle_name').addClass('kmw-disabled');

$('#id_name_details_last_name').addClass('kmw-disabled');

$('#id_name_details_suffix_title').addClass('kmw-disabled');

{% endif %}

{% endif %}


The contents of the 1st if are repeated in the else of the 2nd if statement - I am hoping that I only have to write the repeated contents once.


Aucun commentaire:

Enregistrer un commentaire