Keeping track of changes of HopObject in onPersist()

View: New views
4 Messages — Rating Filter:   Alert me  

Keeping track of changes of HopObject in onPersist()

by Julian Tree :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I find onPerisist trigger very useful.

Is there a way to find out if a key/field has being changed in the  
HopObject?  Like new value of the field vs old(stored) valued of the  
field, like in a stored procedure in at SQL database.

For example something like below would be userful:

this.firstName = req.postParams.firstName;


this.onPersist = function(){
        if(this.firstName.getOldValue() == this.firstName){
                //user has changed the value of the firstName
                // do something big
        }

}


Julian

_______________________________________________
Helma-user mailing list
Helma-user@...
http://helma.org/mailman/listinfo/helma-user

Re: Keeping track of changes of HopObject in onPersist()

by Joshua Paine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>

_______________________________________________
Helma-user mailing list
Helma-user@...
http://helma.org/mailman/listinfo/helma-user

Re: Keeping track of changes of HopObject in onPersist()

by Joshua Paine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 2008-06-16 at 19:06 -0400, Joshua Paine wrote:
> if(this.firstName != this.oldValue('firstname')) doSomething();

Er, this.oldValue('firstName')

--
Joshua Paine <joshua@...>

_______________________________________________
Helma-user mailing list
Helma-user@...
http://helma.org/mailman/listinfo/helma-user

Re: Keeping track of changes of HopObject in onPersist()

by Julian Tree :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Joshua, for the info. I will play with onInit abit more to see  
how it behaves.


On Jun 16, 2008, at 7:06 PM, Joshua Paine wrote:

> 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@...>
>
> _______________________________________________
> Helma-user mailing list
> Helma-user@...
> http://helma.org/mailman/listinfo/helma-user

Julian

_______________________________________________
Helma-user mailing list
Helma-user@...
http://helma.org/mailman/listinfo/helma-user
LightInTheBox - Buy quality products at wholesale price!