Seeking elegance-1

Cubist at aol.com Cubist at aol.com
Fri Jan 14 18:06:26 EST 2005


sez aj445 at traverse.lib.mi.us:
>OK, I've got a stack working, now I want to make it less klunky.
>
>Right now, the user answers questions in a series of Question & Answer
>cards, and when they go to the Summary card, all the questions and 
>responses are listed in one scrolling text field.
>
>It is automatic-- every time the Summary card is opened, there is a new
>summary added on top of the old one, and all are saved.
>
>the script--
>
>on openCard
>   summarize
>   pass openCard
>end opencard
>
>on Summarize
>   repeat with i = 1 to the number of cds of bg "Q&A"
>     put (bg fld "Question" of cd i of bg "Q&A") & return after 
>theSummary
>         put (bg fld "Response" of cd i of bg "Q&A") & return & return
>
>after theSummary
>   end repeat
>   put the long date & return & theSummary before cd fld "Destination"
>
>of cd "List"
>end Summarize
>
>
>Of course, this means that EVERY time the summary page is opened, there
>is a new set appended to the old-- this can add up if you thrash around
>in the stack a while!
   Since your code creates a fresh, shiny new summary every time, why not 
just *put* that summary into the target field? Yes, that would nuke whatever old 
summary used to be there. Is this a problem?

>One solution might be to only do the update if the answers on the Q&A 
>cards have changed.
>
>How would this be done?
   Well, it depends on what you want to do.

Scenario 1: Complete summary every time, old summaries are forgotten
   Just put the fresh summary into the target field and be done with it.

Scenario 2: Complete summary every time, old summaries are NOT forgotten
   Create the fresh summary, put it before the target field.

Scenario 3: Summary consists of ONLY those answers that have changed since 
last time, old summaries are NOT forgotten
   This one needs a little work. Give each answer-card a custom property, 
call it ChangedReply or something. In an openCard handler, save the 
previously-existing answer that *was* on the card; on a closeCard handler, compare the 
saved answer to the *current* answer, and set the ChangedReply of that card 
accordingly. Like so:

local OldAnswer

on openCard
   put field "Answer" into OldAnswer
  -- plus whatever else you do, if anything, in openCard
end openCard

on closeCard
  set the ChangedReply of this card to (field "Answer" <> OldAnswer)
  -- plus whatever else you do, if anything, in closeCard
end closeCard

   If this were HyperCard, you'd probably have all your Answer cards sharing 
a single background, and these handlers would go into that background script. 
This being Rev, the equivalent trick would be to have all the Answer-related 
stuff (fields, etc) in a group which is shared by all your Answer cards, and 
these handlers would go into the scrip of that group.
   So: You've now got ChangedReply properties for all your Answer cards, and 
the ChangedReply property of any given card is True if the user changed his 
answer the last time he visited that card, or False otherwise. With all that in 
mind, your Summarize handler could end up looking like this:

on Summarize
  repeat with i = 1 to the number of cds of bg "Q&A"
    if (the ChangedReply of cd i of bg "Q&A) then
      put (bg fld "Question" of cd i of bg "Q&A") & return after theSummary
      put (bg fld "Response" of cd i of bg "Q&A") & return & return after 
theSummary
    end if
  end repeat
  put the long date & return & theSummary & return & "- - - - - - -" & return 
before cd fld "Destination" of cd "List"
end Summarize

   When that "if" statement in the loop is executed, only those cards for 
which the reply *has* changed, and which therefore have True as the value for 
their ChangedReply property, will show up in the summary.
   Hope this helps...


More information about the use-livecode mailing list