Could anyone explain the term "Specification pattern", applied in Javascript?

8

I did some research on the net, found this website with valuable information. I work a lot with conditionals using javascript, see an excerpt from my function that receives radiogroup as a parameter and verifies whether it was selected yes or no, thus performing the necessary tasks:

    var habilitarCampo3 = function(obj,  selectedRadio)
{
    if(selectedRadio.inputValue == "NAO"){
        Ext.getCmp('comJustificativaAviso').setVisible(true);
        if (iAtividade == 7)
        {
            TransitionData2Form.insertMandatory('comJustificativaAviso','Justifique o não desconto do Aviso Prévio!');

        }
    }
    else{
        Ext.getCmp('comJustificativaAviso').setVisible(false);
        TransitionData2Form.removeMandatory('comJustificativaAviso');
        Ext.getCmp('comJustificativaAviso').setValue(Ext.getCmp('procedeJuridico').getValue().inputValue);

    }
};

Sometimes this code stays in the radiogroup object itself, see:

checkSolicitacoesRealizadas = new Ext.form.RadioGroup
         ({
     width:600,
     fieldLabel:'Solicitações foram realizadas',
     labelSeparator: '?',
     id:'checkSolicitacoesRealizadas',
     value:'ZERO',
      listeners:
              {
                change: function(obj, selectedRadio)
                  {
                    if (selectedRadio.inputValue == "NAO")
                     {
                      alert('É obrigatório a geração das solicitações');
                      Ext.getCmp('checkSolicitacoesRealizadas').setValue('ZERO');
                      }

                  }
              },
     items: [
     {boxLabel: '<b style="color:green;" >Sim</b>', name: 'checkSolicitacoesRealizadas-op', inputValue: "SIM"},
     {boxLabel: '<b style="color:green;" >Sim</b>', hidden:true, name: 'checkSolicitacoesRealizadas-op', inputValue: "ZERO"},
     {boxLabel: '<b style="color:red;" >Não</b>', name: 'checkSolicitacoesRealizadas-op', inputValue: "NAO"}
     ]
         });

Here's one of the hundreds of conditionals in my code:

 if (Ext.getCmp('tipoRescisao').getValue() == 02 && 
              Ext.getCmp('tipoSolicitacao').getValue().inputValue == 'desligamento')
          {
          if (Ext.getCmp('dataInicioAvisoPrevio').getValue() == '')
              {
                      msgErro += "\n É Obrigatório [Data Início Aviso Prévio]!";
                  }
                }
My question is: Is there any Design Patterns that deals with a better way to organize conditional structures by performing some code usage during the field value comparison? I wanted to organize a dynamic and intelligent structure to perform the value comparisons of the form fields. I hope I was clear on the question. Any questions I can improve the question, this is very important to me.

    
asked by anonymous 18.08.2015 / 13:56

3 answers

3

I'll show you how I work with my javascript files:

var ctCompra = {};                                                          // NOVO PADRAO DE DESENVOLVIMENTO        

ctCompra.eventos = function(){                                                      // FUNCAO EVENTOS
    $('#novaautorizacao').button().click(function(){ctCompra.novoFornec();});       // BOTAO NOVA AUTORIZACAO        
    $('#dialog-confirm').hide();                                                    // ESCONDER TELA DE AVISO
    $('button').hide();                                                             // ESCONDER TODOS OS BOTOES
    $("#btEncerrar").button().click(function(){ ctCompra.encerrar(); });            // BOTAO ENCERRAR
    $("#btVoltar").button().click(function(){ ctCompra.retornar(); });              // BOTAO VOLTAR
    $("#mostratodos").button().click(function(){ ctCompra.mostraTodos(); });        // BOTAO MOSTRAR TODOS 
    $("#mostrapendente").button().click(function(){ ctCompra.mostraPendente(); });  // BOTAO MOSTRAR DEPENDENTE        
    $('input:text').setMask();                                                      // CONFIG DE MASKARAS NOS INPUT
    $('#novaautorizacao').show();                                                   // BOTAO NOVA AUTORIZACAO MOSTRAR        
    $('#valor_final').change(function(){ ctCompra.selectPgt(); });                  // CARREGAR PARCELAS
    ctCompra.ajusteSpinner();                                                       // DEPENDENCIA DE EVENTOS
    ctCompra.ajusteTela();                                                          // CHAMADA DA FUNCAO AJUSTE DE TELA
    ctCompra.colorirTr();                                                           // CHAMADA DA FUNCAO COLORIR TR        
};

Now, I declare a variable named ctCompra it works as a dynamic object, I put the name of the attribute and give it a function, eg: ctCompra.events = function () {... When I call ctCompra.eventos () everything inside that function will be called, notice that by "organization" my screen events are all there, and there inside of this function other 3 functions being called:

  • ct.Company.adjustSpinner ();
  • ct.
  • ctCompra.colorirTr ();

And right below this code I write them like this:

ctCompra.ajusteSpinner=function(){                                                  // FUNCAO AJUSTE SPINNER
    ctCompra.parcelas = $('#qtd_parcelas').spinner();                               // BOTAOZINHOS DE INCREMENTAR E DECREMENTAR NO CANTO DO FIELD        
    $('.ui-spinner').removeClass('ui-corner-all').css('float','right');             // STYLE
    $('.ui-spinner').css('margin-right','10px');                                    // STYLE
    ctCompra.parcelas.spinner('disable');                                           // DEFAULT DESABILITADO POIS COMEÇA COM PGT À VISTA QTD=1
    ctCompra.parcelas.spinner({min: 1, max:99});                                    // NÃO ACEITAR VALORES NEGATIVOS NEM ZERO
    $('#select-forma-pgt').change(function(){ ctCompra.selectPgt(); });             // EVENTO SELECIONAR À VISTA OU A PRAZO
    $('.ui-spinner-button').click(function(){ ctCompra.evParcelas(); });            // EVENTO DE CLICAR NA QTD PARCELAS        
};

ctCompra.ajusteTela=function(){                                                     // FUNÇAO AJUSTE DE TELA
    $('#fieldset-menu').css('height','670px');                                      // TELA CONTROLE COMPRA
    $('#fieldset-menu').css('margin','0 70px 10px 0');                              // TELA CONTROLE COMPRA
    $('#div-compromisso').css('padding-top','12px');                                // TELA MENU DE OPÇOES        
};

ctCompra.colorirTr=function(){                                                      // FUNCAO COLORIR TR
    $(".a").mouseover(function(){ $(this).addClass('hover'); });                    // EVENTO MOUSEOVER COLOCAR COR NA TR
    $(".a").mouseout(function(){ $(this).removeClass('hover'); });                  // EVENTO MOUSEOUT RETIRAR COR DA TR  
};

So whenever you need to program inside an "object" so as not to get mixed up with programming, make a code snippet that calls a function from outside to get a lot of function () {... one inside the other .

    
24.08.2015 / 22:47
2

I suggest you stop a lot to reflect on your code and try to separate the responsibilities using the OO concepts, there is no problem in using Specification in javascript since Specification is an "abstract" design pattern, so we would say a kind of philosophy rather than a technology of some specific language, do you understand?

However, it is difficult to answer your question efficiently, as more information is needed on what you are doing.

If it's an integer application in javascript, if it's using javascript as an object-oriented or event. if it's an application in .NET or Java anyway,

I for example use specification pattern in .net next to Domain Driven Design, and the specifications are separated pretty in the domain layer that fire a message queue with an architecture involving n-layer, until arriving at web view.

    
27.08.2015 / 05:15
0

There are several Design Patterns that serve to improve somehow conditional structure. In your example codes I believe that pattern but it is appropriate to bring a better understanding of these structures would be the MVC Pattern or one of its variations.

    
25.08.2015 / 22:04