[General] Storing Values when a Morph is Persisted, How To

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[General] Storing Values when a Morph is Persisted, How To

Philip Weaver
I struggled with a very simple yet important subject and I just want to post
a possible solution here. I wanted to persist some simple values when one of
my morph instances was persisted. I am not using the Widgets class@this
time. My goal is persist some layout constraints on a morph.
The Morph class does extend from lively.data.Wrapper. lively.data.Wrapper
provides methods for setTrait and getTrait which do store and restore
automatically when a Morph is serialized and deserialized.

Another approach may be to use models. Because I'm not really yet using the
Widgets class I don't really have models working yet. There appears to be no
documentation yet for using models except for Examples.js.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://livelykernel.sunlabs.com/pipermail/general/attachments/20090304/7fe1ac2c/attachment.html 


Reply | Threaded
Open this post in threaded view
|

[General] Storing Values when a Morph is Persisted, How To

Robert Krahn
Hi Phil,

the getTrait and setTrait methods are used by shapes, not morphs. All  
instance variables of morphs are persistent a priori. I modified your  
test code a bit:

Morph.subclass("MyPersistedMorph", {
       
        handlesMouseDown : Functions.True,
       
        initialize : function($super) {
               
                console.log('PersistedMorph.initialize');
                $super(new lively.scene.Rectangle(new Rectangle(100, 100, 200, 200)));
               
                this.setFill(Color.green);
                console.log('PersistedMorph.initialize weight: ' + this.weight); //  
will always be undefined because
                                                                        // initialize isn't called for deserialization
        },
       
        onDeserialize: function() {
                console.log('PersistedMorph.onDeserialize weight: ' +  
this.weight); // This works
        },
        onMouseDown : function($super, event) {
               
                console.log('PersistedMorph.onMouseDown');
                console.log('PersistedMorph.onMouseDown weight: ' + this.weight);
                this.weight = 100;
               
                return true;
        }
});


Robert


On Mar 3, 2009,@11:18 PM, Philip Weaver wrote:

> I struggled with a very simple yet important subject and I just want  
> to post a possible solution here. I wanted to persist some simple  
> values when one of my morph instances was persisted. I am not using  
> the Widgets class@this time. My goal is persist some layout  
> constraints on a morph.
>
> The Morph class does extend from lively.data.Wrapper.  
> lively.data.Wrapper provides methods for setTrait and getTrait which  
> do store and restore automatically when a Morph is serialized and  
> deserialized.
>
> Another approach may be to use models. Because I'm not really yet  
> using the Widgets class I don't really have models working yet.  
> There appears to be no documentation yet for using models except for  
> Examples.js.
> _______________________________________________
> General mailing list
> [hidden email]
> http://livelykernel.sunlabs.com/mailman/listinfo/general

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://livelykernel.sunlabs.com/pipermail/general/attachments/20090303/07c53389/attachment.html 


Reply | Threaded
Open this post in threaded view
|

[General] Storing Values when a Morph is Persisted, How To

Philip Weaver
I refocused on your other response about persisting common values but not
common objects. Until I discover how to persist one of my own Object
subclasses, I'm just sort of unpackaging and repackaging my complex objects
on the morph for now and I have that working. I'm probably making things too
difficult by not using models and the Widget class but I'll revisit those
soon. Everything will get easier when I embrace coding directly inside a
World - I keep going back and forth between my local editor and the wiki.

On Wed, Mar 4, 2009@1:46 AM, Robert Krahn <
[hidden email]> wrote:

> Hi Phil,
> the getTrait and setTrait methods are used by shapes, not morphs. All
> instance variables of morphs are persistent a priori. I modified your test
> code a bit:
>
> Morph.subclass("MyPersistedMorph", {
>
> handlesMouseDown : Functions.True,
>
> initialize : function($super) {
>
> console.log('PersistedMorph.initialize');
> $super(new lively.scene.Rectangle(new Rectangle(100, 100, 200, 200)));
>
> this.setFill(Color.green);
> console.log('PersistedMorph.initialize weight: ' + this.weight); // will always be undefined because
>
> // initialize isn't called for deserialization
>
> },
>
>
> onDeserialize: function() {
>
> console.log('PersistedMorph.onDeserialize weight: ' + this.weight); // This works
>
> },
> onMouseDown : function($super, event) {
>
> console.log('PersistedMorph.onMouseDown');
> console.log('PersistedMorph.onMouseDown weight: ' + this.weight);
> this.weight = 100;
>
> return true;
> }
> });
>
>
>
> Robert
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://livelykernel.sunlabs.com/pipermail/general/attachments/20090304/1eac8386/attachment.html