wes wes - 1 month ago 8
Javascript Question

breezejs: reject changes to specific property

Is it possible to reject the changes to a single property of a breeze object without rejecting ALL the changes to that object?

Say I have

// assume manager is an EntityManager containing a number of preexisting entities.
var person = manager.createEntity("Person");
// assume Name and House are valid properties of a Person object
person.Name("Jon Snow");
person.House("Lannister");


But I ONLY want to discard the changes made to the objects
House
property.

Is this possible, if so, how would I go about doing it?

Note: I would rather not iterate of the
originalValues
property and just replace them like that. I guess, I'm looking for a more graceful solution like...

person.House.rejectChanges();


where
rejectChanges()
is called on the property itself or something like that.

Answer

For the lack of a better solution I came up with the following code which seems to serve my purposes:

function RevertChangesToProperty(entity, propertyName) {
  if (entity.entityAspect.originalValues.hasOwnProperty(propertyName)) {
    var origValue = entity.entityAspect.originalValues[propertyName];

    entity.setProperty(propertyName, origValue);
    delete entity.entityAspect.originalValues[propertyName];

    if (Object.getOwnPropertyNames(entity.entityAspect.originalValues).length === 0) {
      entity.entityAspect.setUnchanged();
    }
  }
}