简体   繁体   中英

Determine if the content in a (dojo) dijit.Editor is different from the initial value

I have a form containing many dijit.form elements and also a dijit.Editor. The form is initially filled with data that I get from the server. The user can change the content and then submit this back to the serer. A classic use case.

When the user submits the form I need to only send the changed data. The problem with the dijit.Editor is that it sometimes changes the initial content even if the user did not make any changes. For example:

The initial content entered in the dijit.editor is this:

"Gesegmenteerde rand, 115 mm~Max 13280 U/min, 80 m/sec</br>~Drooggebruik"

And when retrieving the content like this editorObj.get('value'); it returns this:

"Gesegmenteerde rand, 115 mm~Max 13280 U/min, 80 m/sec<br />~Drooggebruik"

As you can see the </br> is changed to <br />. I know the original value is wrong, but that's because the source sucks and that is out of my control.

So my question is: is there an easy way to check if the content has indeed been changed by the user instead of just by dijit.Editor itself.

What I ended up doing is was adding two extra attributes to the Editor:

dijitObj.set('originalValue', value);
dijitObj.set('value', value);
dijitObj.set('uneditedValue', dijitObj.get('value'));

In the read out of the value of the the editor I use these to determine if something changed at all:

var value = dijitObj.get('value');
if (dijitObj.get('uneditedValue') === value) {
  // The value hasn't changed, so we send the original value
  value = dijitObj.get('originalValue');
}

Something like this (on the editor markup) works:

onkeypress="MyObject.setDirty(true);"

And just keep track of it on MyObject.isDirty .

This does have the drawback that if the user types into the editor and then modifies everything to be exactly as it was originally, the value will be wrong (ie, true, whereas content is back to original), but it is sufficient for most purposes.

Well if you just want to check IF the user modified something you could try that :

var editorIsDirty = false;
var someConnect = dojo.connect(myEditor, "onChange", this, function(newValue){
    if(originalSource != newValue){
        editorIsDirty = true;
        return;
    }
    editorIsDirty = false;
});

something like that, you get the idea ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM