Custom equality comparison

Custom equality comparison

sliekenssliekens Posts: 97Questions: 17Answers: 2
edited May 2017 in Feature requests

With Editor, when you submit a form without changing any fields, Editor is smart enough to not submit the data. This is great, but I'd like more control over how Editor decides when values are changed.

I suggest introducing a field.changeDetection callback function that Editor can invoke with the old and the new field value when submitting a form.

changeDetection: function(oldValue, newValue) {
  // returns true to indicate a change is detected; otherwise, false
}

Example usage:

var editor = new $.fn.Editor( {
    ajax:   "php/staff.php",
    table:  "#myTable",
    fields: [
        {
            label: "First name:",
            name: "first_name",
            changeDetection: function(oldValue, newValue) {
              if (oldValue.trim() === newValue.trim()) {
                // unchanged
                return false;
              } else {
                return true;
              }
            }
        }
    ]
} );

Example usage 2:

// treat empty strings and white space as null
function changeDetection(oldValue, newValue) {
  if (oldValue == null && newValue.trim() === '') {
      return false;
    } else {
      return newValue !== oldValue;
    }
  }
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Answer ✓

    Thanks for the suggestion. This might have been useful in another recent thread where loose type checking was wanted rather than strict.

    I've added it to my list of future features.

    Regards,
    Allan

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Just to say that Editor 1.7 will have a fields.compare option which can be used to determine if the field should be submitted to the server or not.

    Regards,
    Allan

This discussion has been closed.