How to reload grid without having to check if it exists JS JQuery

0

The system was built with modal and it is possible that there is a modal there in the 2nd level that needs to update two grids below (one in the main screen and another in the modal in the 1st level), but not always this modal is called from the same place so when you finish the action it is possible that these two grids do not exist below it, so every time we use grid.fnDraw() in JS we need to first check if the grid in question exists with if(typeof(grid) != "undefined" .

I would like an option to no longer need to check every time I reload a grid if it exists.

I thought of something like creating a method like this:

function recarregarGrid(grid) {
   if (typeof (grid) != "undefined")
       grid.fnDraw();
}

Therearecaseswherewereloadseveralgridsthatmaynotexistatthattime:

//Concluíuumaaçãoeprecisaatualizarosdadosdasgridsabaixodamodalif(typeof(gridItem)!="undefined")
  gridItem.fnDraw();
if(typeof(gridFornecedor) != "undefined")
  gridFornecedor.fnDraw();
if(typeof(gridLocalEntrega) != "undefined")
  gridLocalEntrega.fnDraw();
if(typeof(gridStackOverflow) != "undefined")
  gridStackOverflow.fnDraw();

But anyway I can not pass an undefined variable by parameter. So I need a way to be able to pass the grid as a parameter even if it is as "undefined" or a better solution so that you no longer have to use that if before reloading the grid.

    
asked by anonymous 28.10.2016 / 14:37

2 answers

1

If you are running from the browser, you can use the global object 'window' to see if a particular global variable exists

function recarregarGrid(nome) {
    window[nome] && window[nome].fnDraw();
}

recarregarGrid('exemplo')

Note that in the example above, 'example' is a string containing the variable name.

Or, if you do not want to pollute the global scope, you can use a single global variable to represent all grids, but in this case you need to remember to always register each grid when it is first loaded: / p>

gridsExistentes = {};

function recarregarGrid(nome) {
    gridsExistentes[nome] && gridsExistentes[nome].fnDraw();
}

function registrarGrid(nome, grid) {
    gridsExistentes[nome] = grid;
}

recarregarGrid('exemplo')

// (...)

registrarGrid('exemplo', gridPreviamenteCarregado);
recarregarGrid('exemplo')
    
28.10.2016 / 15:53
3

Edited after comment: You can initialize grids at the beginning of the script as a global object that will gather all the grids that exist. Whenever you define a grid, create a property within that object to save. So when you call recarregarGrids() just iterate over the existing grids there and call the function.

// Guarda um objeto para todos os grids
var grids = grids || {};

// Define a função de forma genérica, que vai iterar sobre os grids
// adicionados ao objeto
recarregaGrids = function() {
  for( var grid in grids ) {
    if ( grids.hasOwnProperty(grid) ) {  
        console.log(grids[grid]); // apenas para testar o valor de cada grid
        // grids[grid].fnDraw();
    }
  }
}

// Quando estiver definindo as grids, guarde todas elas dentro do objeto master
grids.grid1 = 'gridStackOverflow';
grids.grid2 = 'gridPadrao';
//etc

recarregaGrids();
    
28.10.2016 / 15:08