layout within an object
[1/4] from: mgh520::yahoo at: 30-Jul-2001 21:52
I got this tip from the REBOL/View developer's guide. It suggest defining a layout inside
of an object to
keep variables local and avoid confusion. I like this approach as it seems clean.
However, in the script at the end of this email, an instance variable (name) gets set
after the object is
created, and if you "print myview/name" you get the value you'd expect, i.e., Nelson
Muntz.
But once the view is displayed, if you click the Print button, it will print the original
value of the variable,
Edna Crabapple. It seems like the block for the Print button action is being evaluated
when it's created
and then not evaluated again when it's actually clicked.
I figured out that if I wrap the layout definition inside of a block [layout [size 640x480
....] then I can use
view do [myview/page-layout]
and get the correct results, which I believe further supports my theory that
the action is being evaluated once at the start. According to the developer's guide,
you can wrap things in
a layout with a do [...] to cause them to be evaluated once at creation time. But this
really seems to be the
default.
Can anyone explain this behavior or should I just use the block techinque? What I haven't
show yet, is
what the value of name is once the view is closed. But I didn't know how to do that--I'm
still new at this.
Thanks in advance for any help. I hope to learn enough soon to be able to answer other's
questions, as
everyone has been so helpful on this list. Thanks.
Mike
REBOL []
MyObject: make object! [
name: "Edna Crabapple"
page-layout: layout [
size 640x480
below
h2 "Test"
return
button "Print Value" [print ["name in button action:" name]]
]
]
myview: make MyObject []
myview/name: "Nelson Muntz"
print myview/name
view myview/page-layout
[2/4] from: sanghabum:aol at: 31-Jul-2001 4:48
[mgh520--yahoo--com] writes:
<snips>
> However, in the script at the end of this email, an instance variable
(name)
> gets set after the object is
> created, and if you "print myview/name" you get the value you'd expect,
i.e.,
> Nelson Muntz.
> But once the view is displayed, if you click the Print button, it will
print
> the original value of the variable,
> Edna Crabapple. It seems like the block for the Print button action is
> being evaluated when it's created
> and then not evaluated again when it's actually clicked.
Hi Mike,
I don't think I've ever quite got the hang of Rebol Contexts. And I'm still
not sure if that's me being thick or Rebol being inconsistent. But there is
a simple workaround for your code.
I always fully name variables (and function names) in VID action facets and
code the generally does what I expect. In your case, try changing:
button "Print Value" [print ["name in button action:" name]]
to
button "Print Value" [print ["name in button action:" myobject/name]]
That way there is no ambiguity in the name of what you are printing. (Where
the ambiguity came from in this instance is still a mystery to me).
--Colin.
[3/4] from: brett::codeconscious::com at: 31-Jul-2001 21:27
Hi Mike,
> However, in the script at the end of this email, an instance variable
(name) gets set after the object is
> created, and if you "print myview/name" you get the value you'd expect,
i.e., Nelson Muntz.
Actually you are creating two objects - one referred to as "MyObject" and
another referred to as "myview".
> But once the view is displayed, if you click the Print button, it will
print the original value of the variable,
> Edna Crabapple. It seems like the block for the Print button action is
being evaluated when it's created
> and then not evaluated again when it's actually clicked.
>
> I figured out that if I wrap the layout definition inside of a block
[layout [size 640x480 ....] then I can use
> "view do [myview/page-layout]" and get the correct results, which I
believe further supports my theory that
> the action is being evaluated once at the start. According to the
developer's guide, you can wrap things in
> a layout with a do [...] to cause them to be evaluated once at creation
time. But this really seems to be the
> default.
It certainly appears that way - but there is an alternative. Both of your
objects share the same page-layout object which ultimately (deeper down)
contains the button action - in reality a Rebol function. Thus both objects
share the same functionality.
So you have two objects. They have different "name" fields but share the
exact same page-layout
>> same? myview MyObject
== false
>> same? myview/name MyObject/name
== false
>> type? myview/page-layout
== object!
>> same? myview/page-layout MyObject/page-layout
== true
So when you set the name to "Nelson Muntz" you are affecting the myview
field name but nothing refers to it.
For kicks, try your code again but instead of setting myview/name to
Nelson...
set Myobject/name instead like this:
myobject/name: "Nelson Muntz"
Bye bye Edna...
So you see that the function works, but it is looking at a "name" different
to what you were expecting.
The short answer, which I'm not entirely sure if I'll get away with is, you
don't need to make myview just use MyObject :)
Now before you ask, because I don't think I'll get away with the short
answer, how you can have two windows share the same layout but act on their
own data try this (only one of multiple ways):
context-spec: [
name: "Edna Crabapple"
page-layout: layout [
size 640x480
below
h2 "Test"
return
button "Print Value" [print ["name in button action:" name]]
]
]
MyObject: make object! context-spec
myview: make object! context-spec
myview/name: "Nelson Muntz"
print myview/name
view myview/page-layout
view myobject/page-layout
and see this:
>> same? myview/page-layout myobject/page-layout
== false
HTH
Brett.
[4/4] from: mgh520:y:ahoo at: 31-Jul-2001 9:11
Thanks Brett. And thanks Colin. I knew that two objects were being created, but thought
only the second one would
be used. Anyway, I like this approach of defining the "context-spec", because 1, it
works, and 2, it doesn't involve the
creation of an object that I was never planning to use anyway. Thanks!
Mike
7/31/2001 6:27:53 AM, "Brett Handley" <[brett--codeconscious--com]> wrote: