Am 06.12.17 um 11:34 schrieb Peter Uhnák:
> Hi,
> 
> I'm trying to get dependent magritte descriptions, but it seems like this
> is not something that can be done in the descriptions:
> 
> Imagine a class Contact with "city" and "country" descriptions.
> I would like to populate the city description based on the currently
> selected country
> 
> ~~~~~~~~~~~~~~~~~~~~~~
> descriptionCountry
> <magritteDescription>
> ^ MASingleOptionDescription new
> accessor: #country;
> label: 'Country';
> options: self countries;
> yourself
> 
> descriptionCity
> <magritteDescription>
> ^ MASingleOptionDescription new
> accessor: #city;
> label: 'City';
> options: (self citiesInCountry: self country);
> yourself
> ~~~~~~~~~~~~~~~~~~~~~~
> 
> The problem with this approach is that both descriptions are in the same
> form, so I need to update the cities list even before the country is saved
> back into the object.
> 
> Is this possible in magritte?

I had the same wish and came up with the following solution:

--- It will calculate the options with a block instead of an already
given collection.

--- It will recalculate (aka "not cache") the options if you hit the
"Save" button on the magritte form.

--- It will NOT recalculate the options on the fly when the other
dropdown box has been selected. This probably could be done with some
ajax stuff or a forced HTTP form send.

--- I use my own subclasses for magritte options, for example:

MASingleOptionDescription subclass: #ALMaSingleModelOptionDescription
        uses: TALMagritteOptionDescription
        instanceVariableNames: ''
        classVariableNames: ''
        package: 'Albus-Magritte'

--- See attached fileout of the Trait TALMagritteOptionDescription.

--- You can then describe an option as follows:

descriptionYourAttribute
        <magritteDescription>
        ^ALMaSingleModelOptionDescription new
                accessor: #yourAccessor;
                optionsBlock: ["stuff to calculate your collection"];
                label: 'Your label';
                yourself




Cheers, Andreas


-- 
Andreas Brodbeck
www.mindclue.ch
'From Pharo6.0 of 13 May 2016 [Latest update: #60520] on 6 December 2017 at 
2:44:57.188493 pm'!
Trait named: #TALMagritteOptionDescription
        uses: {}
        category: 'Albus-Magritte'!

!TALMagritteOptionDescription methodsFor: 'as yet unclassified' stamp: 
'AndreasBrodbeck 4/11/2017 08:49'!
options
        | optionCollection isCachingOptions |
        "So, dass wir auch einen Block als Options angeben können, besser für 
Performance"
        
        optionCollection := super options.
        
        isCachingOptions := self propertyAt: #isCachingOptions ifAbsent: [true].
        
        "Einmaliges umwandeln von Block in Collection"
        optionCollection isBlock ifTrue: [
                | evaluatedBlock |
                evaluatedBlock := optionCollection value.
                isCachingOptions
                        ifTrue: [
                                optionCollection := evaluatedBlock.
                                self options: optionCollection]
                        ifFalse: [
                                ^evaluatedBlock]
        ].
        
        ^optionCollection! !

!TALMagritteOptionDescription methodsFor: 'as yet unclassified' stamp: 
'AndreasBrodbeck 4/18/2017 12:20'!
favorites
        ^self propertyAt: #favorites! !

!TALMagritteOptionDescription methodsFor: 'as yet unclassified' stamp: 
'AndreasBrodbeck 4/11/2017 08:50'!
beNotCachingOptions
        self propertyAt: #isCachingOptions put: false! !

!TALMagritteOptionDescription methodsFor: 'as yet unclassified' stamp: 
'AndreasBrodbeck 4/11/2017 08:57'!
optionsBlock: aBlock
        self options: aBlock.
        self beNotCachingOptions.! !

!TALMagritteOptionDescription methodsFor: 'as yet unclassified' stamp: 
'AndreasBrodbeck 4/18/2017 12:21'!
hasFavorites
        ^self favorites notBlank! !

!TALMagritteOptionDescription methodsFor: 'as yet unclassified' stamp: 
'AndreasBrodbeck 4/18/2017 12:20'!
favorites: aCollection
        self propertyAt: #favorites put: aCollection! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

TALMagritteOptionDescription classTrait
        uses: {}!

Reply via email to