[Cuis-dev] How to use Events?

Juan Vuletich juan at jvuletich.org
Mon Sep 9 06:04:05 PDT 2019


On 9/9/2019 3:17 AM, Philip Bernhart via Cuis-dev wrote:
> Hello,
>
> I noticed that when I register events with when:send:to:with: with collections
> as the with: paramater the contents of the collection the collection
> becomes nil on the actually triggered event. Maybe that has something
> todo with WeakMessageSend. Anyway an example tells better what I mean:
>
>
> browserMorph
> 	setProperty: #editItemAction
> 	toValue: [:repository :record|
> 		| flashcardMorph card mapping |
> 		flashcardMorph _ FlashcardMorph new.
> 		card _ Flashcard new.
> 		card question: (record at: #question ifAbsent: '').
> 		card answer: (record at: #answer ifAbsent: '').
> 		
> 		card when: #changed
> 		     send: #saveRecordMappingWith:
> 		     to: browser
> 		     with: {card. record}.
>
> 		flashcardMorph model: card.
> 		flashcardMorph showAnswer.
> 		flashcardMorph enableEditing.
> 		flashcardMorph openInWorld ].
>
> Then in Flashcard there is a foo method which does:
> self triggerEvent: #changed.
>
> Here the {card. record} becomes for some reason (weak reference?) nil
> and causes that the event isn't triggered. So #saveRecordMappingWith:
> isn't called.
>
> With simple datatypes like Integers this works, with static arrays this
> seems to work too.
>
> Maybe this style of doing that isn't the right one, maybe I'm using the
> event system for things which it wasn't supposed to do.
>
> Suggestions?
>
>
> Thanks for your time,
> Philip

Hi Philip,

I believe the event system uses WeakMessageSend in all Smalltalk 
dialects. Folks, please correct me if I'm wrong.

An easy way to fix the problem is to turn your collection into a 
literal, using backticks:

		card when: #changed
		     send: #saveRecordMappingWith:
		     to: browser
		     with: `{card. record}`.


Cheers,

-- 
Juan Vuletich
www.cuis-smalltalk.org
https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev
https://github.com/jvuletich
https://www.linkedin.com/in/juan-vuletich-75611b3
@JuanVuletich



More information about the Cuis-dev mailing list