I have a question using underscore isEqual to compare two JSON strings. Currently i have done an app in backbone, and I'm using _.isEqual(savedModel.toJSON(),changedModel.toJSON() ) to detect if the model has changed in the page and promt a "You have unsaved changes, do you want to save?" dialog if the user tires to navigate away.
For some reason I get the dialog in random places even though I have done nothing or have saved changes. Debugging is driving me crazy.
Could this be because JSON does not guarantee the order of the objects in the JSON and underscores isEqual does not handle this case properly? So even if the models are the same, some attributes in the JSON might be different and it returns false?
Pseudocode:
//when entering the page the original model is cloned, when user does changes to the
//page, the model is cloned again
var savedModel = currentModel.clone().toJSON();
//when the user tries to navigate away from the page
if( _.isEqual(savedModel, model.toJSON() ){
showSavePromptDialog();
}