Two related dependent fields looping through each other

Two related dependent fields looping through each other

dan@19ideas.comdan@19ideas.com Posts: 56Questions: 13Answers: 1
edited June 2018 in Editor

I have two fields in the editor, where they are each dependent on each other. The fields are Altitude and Atmospheric Pressure. The way this works is that some users that visit the page would require to enter in either Altitude, or Atmospheric Pressure, and the field would be updated based on what the user inputted. So I built a system to do some math where it would convert the numbers based on an ajax request.

So the below code makes a call to the webservice, in order to update the Altitude field:

      _editor.dependent('AtmosphericPressure', function (val, data, callback) {
        $.ajax({
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
          url: '/api/cf/ap',
          type: 'GET',
          datatype: 'JSON',
          data: {
            val: val
          },
          success: function (json) {
            callback(JSON.parse(json));
          }
        });
      });

The callback json from above looks like this:

      {
           "values": { "Altitude": "0" }
      }

So the below code makes a call to the webservice, in order to update the AtmosphericPressure field:

      _editor.dependent('Altitude', function (val, data, callback) {
        $.ajax({
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
            url: '/api/cf/alt',
            type: 'GET',
            dataType: 'JSON',
            data: {
              val: val
            },
            success: function (json) {
              callback(JSON.parse(json));
            }
          });
      });

the callback json from above looks like this:

      {
           "values": { "AtmosphericPressure": "14.696" }
      }

But because it worked out like this, it keeps going back and forth in an never-ending loop. Is there a way to prevent this? I got several other fields that this needs to happen to as well. It kind of performs as a calculator where all information of each data conversion is important to show.

I'm thinking one solution is to check for dirty data and do an ajax request based on that, but I have no idea how to check if the data is dirty.

Answers

  • dan@19ideas.comdan@19ideas.com Posts: 56Questions: 13Answers: 1

    I figured it out.. I save a state of data when I selected a record, then I compare that state to the value:

    _editor.dependent('Altitude', function (val, data, callback) {
        if (val != vm.globalState.Altitude)
        {
             vm.globalState.Altitude = val;
            $.ajax({
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                url: '/api/cf/alt',
                type: 'GET',
                dataType: 'JSON',
                data: {
                    val: val
                },
                success: function (json) {
                    callback(JSON.parse(json));
                }
            });
        }
    });
    

    So the "if-statement" acts as a gateway, ending the loop. And I do this for every dependent field.

This discussion has been closed.