Saving model objects

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Saving model objects

Rogier Schaaf
Hi,

How do I process a click on a Save button with a Magrite built form? Using a manually created Seaside form I would register a callback, like:

html anchor
cssClass: 'btn btn-success';
callback: [ self save ];
with: 'Save'

I understand that Magritte doesn't work directly on my model objects but rather with Mementos to process changes. I browsed the class hierarchy of MAMemento however it's not obvious how to make this work with my model.

I use Voyage to persist my model objects in a Mongo database, so my model objects respond to the #save message. Ultimately I need to execute that method to persist changes.

I suspect I need to implement a custom Memento subclass and somehow tie that to my model object but I'm not sure that is the right way. Maybe someone can shed some light on this? I've tried reading up on this but the various bits of documentation I found were lacking code samples.

Thanks.

Roger

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Saving model objects

Mariano Martinez Peck
When you do a #addValidatedForm, it ends up doing #addForm, which ends up being:

addForm
self addForm: #( save cancel )

By default, those will be mapped directly to a method called that way, that is, methods #save and #cancel. Those methods are sent to MAContainerComponent.
Now, see MAContainerComponent:

save
self validate ifFalse: [ ^ self ].
self commit; answer: self model

So...as you can see, indeed, the save is what takes the memento, validates it, and if correct, applies the changes from the memento to the target object. So the result of the #save will be (if success) a modified model object.

The way to persist in a database upon a save for example is to do this. Imagine you are in whatever component from your app. And you do:

newComponent := aDomainObject asComponent
addValidatedForm;
yourself. (self call: newComponent) ifNotNilDo: [ :value | self saveObjectToDatabase: value. ]

If you read seaside doc, you will see this code "(self call: newComponent)" does an answer of the domain object (see highligthed line above in #save), so the answer from "(self call: newComponent)" is either the updated object or nil (if clicked cancel).

Is it clear?

Cheers,


On Thu, May 28, 2015 at 6:08 AM, Rogier Schaaf <[hidden email]> wrote:
Hi,

How do I process a click on a Save button with a Magrite built form? Using a manually created Seaside form I would register a callback, like:

html anchor
cssClass: 'btn btn-success';
callback: [ self save ];
with: 'Save'

I understand that Magritte doesn't work directly on my model objects but rather with Mementos to process changes. I browsed the class hierarchy of MAMemento however it's not obvious how to make this work with my model.

I use Voyage to persist my model objects in a Mongo database, so my model objects respond to the #save message. Ultimately I need to execute that method to persist changes.

I suspect I need to implement a custom Memento subclass and somehow tie that to my model object but I'm not sure that is the right way. Maybe someone can shed some light on this? I've tried reading up on this but the various bits of documentation I found were lacking code samples.

Thanks.

Roger

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki



--

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Saving model objects

Rogier Schaaf
Completely clear. Thanks for your help!

On 28 May 2015 at 13:08, Mariano Martinez Peck <[hidden email]> wrote:
When you do a #addValidatedForm, it ends up doing #addForm, which ends up being:

addForm
self addForm: #( save cancel )

By default, those will be mapped directly to a method called that way, that is, methods #save and #cancel. Those methods are sent to MAContainerComponent.
Now, see MAContainerComponent:

save
self validate ifFalse: [ ^ self ].
self commit; answer: self model

So...as you can see, indeed, the save is what takes the memento, validates it, and if correct, applies the changes from the memento to the target object. So the result of the #save will be (if success) a modified model object.

The way to persist in a database upon a save for example is to do this. Imagine you are in whatever component from your app. And you do:

newComponent := aDomainObject asComponent
addValidatedForm;
yourself. (self call: newComponent) ifNotNilDo: [ :value | self saveObjectToDatabase: value. ]

If you read seaside doc, you will see this code "(self call: newComponent)" does an answer of the domain object (see highligthed line above in #save), so the answer from "(self call: newComponent)" is either the updated object or nil (if clicked cancel).

Is it clear?

Cheers,


On Thu, May 28, 2015 at 6:08 AM, Rogier Schaaf <[hidden email]> wrote:
Hi,

How do I process a click on a Save button with a Magrite built form? Using a manually created Seaside form I would register a callback, like:

html anchor
cssClass: 'btn btn-success';
callback: [ self save ];
with: 'Save'

I understand that Magritte doesn't work directly on my model objects but rather with Mementos to process changes. I browsed the class hierarchy of MAMemento however it's not obvious how to make this work with my model.

I use Voyage to persist my model objects in a Mongo database, so my model objects respond to the #save message. Ultimately I need to execute that method to persist changes.

I suspect I need to implement a custom Memento subclass and somehow tie that to my model object but I'm not sure that is the right way. Maybe someone can shed some light on this? I've tried reading up on this but the various bits of documentation I found were lacking code samples.

Thanks.

Roger

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki



--

_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki


_______________________________________________
Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Loading...