1
votes

I have array with names of, lets say, dependent objects. The problem is that the dependent object can also have dependent objects and so on. My code is only 2 levels deep and i need to implement a recursive way of doing it. My code is :

if (obj.detailsObjects) {
            _.forEach(obj.detailsObjects, function (o, key) {
                if (window[o] instanceof QuadTable) {
                    //limpamos o details e reiniciamos a info
                    if ($.fn.DataTable.isDataTable('#' + window[o].tableId)) {
                        debugger;
                        window[o].tbl.clear();
                        window[o].tbl.columns.adjust().draw();

                        $('#' + window[o].tableId + "_info > .nRecords").text(window[o].tbl.data().count() + " " + window[o].i18nEntries.record);
                        if (window[o].editorXt) {
                            $('#' + window[o].tableId + '_xtForm')[0].reset();
                        }
                    }
                    //se o details for um master repetimos a operação para o details do details
                    //todo recursividade
                    if (window[o].detailsObjects) {
                        _.forEach(window[o].detailsObjects, function (ob, key) {
                            if (window[ob] instanceof QuadTable) {
                                if ($.fn.DataTable.isDataTable('#' + window[ob].tableId)) {
                                    window[ob].tbl.clear();
                                    window[ob].tbl.columns.adjust().draw();
                                    $('#' + window[ob].tableId + "_info > .nRecords").text(window[ob].tbl.data().count() + " " + window[ob].i18nEntries.record);
                                    if (window[ob].editorXt) {
                                        $('#' + window[ob].tableId + '_xtForm')[0].reset();
                                    }
                                }
                            } else if (window[ob] instanceof QuadForm) {
                                window[ob].clearForm();
                            }
                            window[ob].startIn = 0;
                        });
                    }
                } else if (window[o] instanceof QuadForm) {
                    window[o].clearForm();
                }
                window[o].startIn = 0;
            });
        }
1

1 Answers

1
votes
function recursiveFuntcion(obj) {
  // base condition
  if (!obj || !obj.detailsObjects)
    return;
  _.forEach(obj.detailsObjects, function(o, key) {
    if (window[o] instanceof QuadTable) {
      //limpamos o details e reiniciamos a info
      if ($.fn.DataTable.isDataTable('#' + window[o].tableId)) {
        debugger;
        window[o].tbl.clear();
        window[o].tbl.columns.adjust().draw();

        $('#' + window[o].tableId + "_info > .nRecords").text(window[o].tbl.data().count() + " " + window[o].i18nEntries.record);
        if (window[o].editorXt) {
          $('#' + window[o].tableId + '_xtForm')[0].reset();
        }
      }
      //recursive call
      recursiveFunction(window[o]);
    } else if (window[o] instanceof QuadForm) {
      window[o].clearForm();
    }
    window[o].startIn = 0;
  });
}