[Helma-user] Keeping track of changes of HopObject in onPersist()

Joshua Paine joshua at papercrown.org
Tue Jun 17 01:06:06 CEST 2008


On Mon, 2008-06-16 at 16:18 -0400, Julian Tree wrote:
> Is there a way to find out if a key/field has being changed in the  
> HopObject?

Not in Helma 1.x. I heard there were more events planned for Helma NG,
so maybe there's an onChange event planned for HopObject properties, but
I don't know.

> if(this.firstName.getOldValue() == this.firstName){

This syntax would require deep magic, since method getOldValue would
normally be obliterated as soon as you assigned something to firstName.

If HopObject.onInit works as specified, you could add these methods to
HopObject:

function onInit(){
  var oldVals = {};
  for(let name in this) {
    if(!(this[name] instanceof HopObject ||
         this[name] instanceof Function)) {
      oldVals[name] = this[name];
    }
  }
  this.cache.oldVals = oldVals;
}

function oldValue(name){
  if(!this.cache.oldVals) return;
  return this.cache.oldVals[name];
}

Then in your onPersist function you can write:

if(this.firstName != this.oldValue('firstname')) doSomething();

My sample code will only work for simple properties (not
HopObjects/collections). And only if onInit works as advertised. I had
some trouble with it the only time I tried to use it, but that was my
first app and using the object db. I haven't revisited it since.

-- 
Joshua Paine <joshua at papercrown.org>



More information about the Helma-user mailing list