MVP : how to get the selection from a ListModel.

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

MVP : how to get the selection from a ListModel.

Dominique Dartois-2
Hi,
I have a model like this:
MyList>initialize
    self tc: String new.   "no associated presenter".
    self listeTc: OrderedCollection new.
    self listeTc add: 'aaaaa'.

A view with a ListPresenter + Ok Cancel.

A presenter like:
MyPresenter>model: param
    |aspectBuffer|
    super model: param.
    aspectBuffer := self model.
    listeTcPresenter model: (ListModel on: aspectBuffer listeTc).

I want to set tc with the selection of the model.
I tried (among a lot of methods):
MyPresenter>apply
    self model tc: listeTcPresenter selection.
    super apply.

The variable tc is never changed in the model. In fact I have other controls
on this dialog and they work ok.
It seems I cannot update this variable of the model because I don't have an
associated presenter.

I'm stuck.

---
Dominique Dartois


Reply | Threaded
Open this post in threaded view
|

Re: MVP : how to get the selection from a ListModel.

Schwab,Wilhelm K
Dominique,

> Hi,
> I have a model like this:
> MyList>initialize
>     self tc: String new.   "no associated presenter".
>     self listeTc: OrderedCollection new.
>     self listeTc add: 'aaaaa'.
>
> A view with a ListPresenter + Ok Cancel.
>
> A presenter like:
> MyPresenter>model: param
>     |aspectBuffer|
>     super model: param.
>     aspectBuffer := self model.
>     listeTcPresenter model: (ListModel on: aspectBuffer listeTc).
>
> I want to set tc with the selection of the model.
> I tried (among a lot of methods):
> MyPresenter>apply
>     self model tc: listeTcPresenter selection.
>     super apply.
>
> The variable tc is never changed in the model. In fact I have other controls
> on this dialog and they work ok.

The problem is that you have taken a one-time snapshot of the selection.
     You will want to "express interest" in the #selectionChanged (???)
event from the list presenter.  Something like the following:

createSchematicWiring
    super createSchematicWiring.
    listeTcPresenter
       when:#selectionChanged
       send:#tcSelectionChanged
       to:self.

Then

MyPresenter>>tcSelectionChanged will be called when the list selection
changes.  Then

tcSelectionChanged
    self model tc:listeTcPresenter selection.

should do what you want, as it will be called each time the selection
changes.  Note that none of the above is tested, and that incorrect
"spelling" of the selectors can lead to silent bugs.

This can be very confusing at first.  One day you will wake up and
wonder why it was so difficult to grasp - I realize that isn't much help
now =:0


> It seems I cannot update this variable of the model because I don't have an
> associated presenter.

I doubt that is your problem, but a mis-match in view names in
#createComponents and the names in the view resource can cause some very
interesting bugs.

Dolphin's MVP framework gets a lot of flexibility from being able to
ignore missing views.  FWIW, I approve of it, and would not change the
default.  However, I have also long wanted an optional feature to treat
a missing subview as an error per overridable method in Presenter, or
something like that.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: MVP : how to get the selection from a ListModel.

Dominique Dartois-2
Bill,
It doesn't seem to work.
I created a minimal application an I see the same problem:

Model>>TinyApp>>initialize
    super initialize.
    self tc: String new.
    self tcList: OrderedCollection new.
    self tcList add: 'Choice 1'.
    self tcList add: 'Choice 2'.

Dialog>>TinyAppPresenter>>createComponents
    super createComponents.
    tcListPresenter := self add: ListPresenter new name: 'tcList'.

Dialog>>TinyAppPresenter>>model: aList
    |aspectBuffer|
    super model: aList.
    aspectBuffer := self model.
    tcListPresenter model: (ListModel on: aspectBuffer tcList).

Dialog>>TinyAppPresenter>>createSchematicWiring
    super createSchematicWiring.
    tcListPresenter when: #selectionChanged send: #tcSelectionChanged to:
self.

Dialog>>TinyAppPresenter>>tcSelectionChanged
    self model tc: tcListPresenter selection.

In a workspace, I execute :
ta := TinyApp new. <Evaluate It>
TinyAppPresenter showOn: ta <Evaluate It>
ta tc <Display It> --> An empty string

Maybe "showOn:" passes a copy of the model?

----
Dominique Dartois


Reply | Threaded
Open this post in threaded view
|

Re: MVP : how to get the selection from a ListModel.

Ian Bartholomew-21
Dominique,

I'm not completely sure what you are trying to achieve?  I think you
want the current selection of the list copied into the model's tc
variable when Ok is clicked in the dialog but not when Cancel is clicked?

If so, the having the following three methods in TinyAppPresenter seems
to work.  You just initialize the list from the model when the dialog is
opened and copy back into the subject (the original model) when the
dialog sees #apply.

TinyAppPresenter>>apply
        self subject tc: tcListPresenter selectionOrNil.
        super apply

TinyAppPresenter>>createComponents
        super createComponents.
        tcListPresenter := self add: ListPresenter new name: 'tcList'

TinyAppPresenter>>model: aTinyApp
        super model: aTinyApp.
        tcListPresenter list: aTinyApp tcList

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: MVP : how to get the selection from a ListModel.

Dominique Dartois-2
In article <[hidden email]>, Ian Bartholomew wrote:
> I'm not completely sure what you are trying to achieve?  I think you
> want the current selection of the list copied into the model's tc
> variable when Ok is clicked in the dialog but not when Cancel is clicked?

Yes, absolutely!

> TinyAppPresenter>>apply
> self subject tc: tcListPresenter selectionOrNil.
> super apply

It works!
The big difference is that you use "self subject" and not "self model" as I did.
I understood that #apply applied modifications to the original model when Ok was clicked.
When are the mofication copied back to the original model? Only when the values of the model
are modified by a Presenter?

Thank you.

----
Dominique Dartois


Reply | Threaded
Open this post in threaded view
|

Re: MVP : how to get the selection from a ListModel.

Ian Bartholomew-21
Dominique,

> The big difference is that you use "self subject" and not "self model" as I did.
> I understood that #apply applied modifications to the original model when Ok was clicked.
> When are the mofication copied back to the original model? Only when the values of the model
> are modified by a Presenter?

nb: I have to relearn how this all works every time that I want to
understand what's happening - so the following is a precis only :-).

When you use an AspectBuffer it makes a copy of the object it is
buffering.  The original is held in the #subject instVar and the copy is
held in the #subjectCopy instVar (and is accessed via the value accessor).

The dialog only accesses the copy of the original and all messages
to/from the dialog to the model are passed, by the AspectBuffer, on to
the copy using the #doesNotUnderstand error mechanism.

When the Ok button is pressed the AspectBuffer's #apply method copies
all the _registered_ aspects (ones that were selected using the
#aspectValue in the presenter's #model: method) from the copy back into
the original object.

This mechanism doesn't work automatically with ListModels so you have to
do your own copying of the required bits (the original list going in and
the selection going out).

--
Ian

Use the Reply-To address to contact me (limited validity).
Mail sent to the From address is ignored.


Reply | Threaded
Open this post in threaded view
|

Re: MVP : how to get the selection from a ListModel.

Dominique Dartois-2
Ian, thank you very much for your explanation.
I saw the #doesNotUnderstand error mechanism during my debugging sessions but
its meaning is clearer now.

Thanks.
----
Dominique Dartois