I'll expand sri's reply a little. In your model:

sub save {
   ...
   return $self->pg->db->update_p(...)
}



and in your controller:

sub my_route {
    my $c = shift;
    ...

   $c->model->save(...)  ## save now returns a promise
    ->then(sub { ... render success here })
    ->catch(sub { ... render failure here });
}


This keeps the concerns of rendering out of your model.

Scott

On Monday, September 24, 2018 at 5:00:41 AM UTC-6, Nacho B wrote:
>
> Hi!
>
> As some of my controllers are getting too fat, I am relaying some things 
> to models, starting with the database queries.
>
> Even when saving data I need to return something, an error or a true. But 
> I don't want to render from the callback in the model. I would like to 
> return the output in the callback to the controller, but I don't know how.
>
> I am using the controller-model structure from the blog example (
> https://github.com/mojolicious/mojo-pg/tree/master/examples/blog). It's 
> very clean and easy to understand, but I don't know if the non-blocking way 
> can fit in it…
>
>
>    
> // in the 'party' model
>
> sub save {
>   my ($self, $id, $party) = @_;
>   $self->pg->db->update('party_t', $party, {id => $id} => sub {
>     my ($db, $err, $results) = @_;
>     if $err {
>       // ...  must I render here?? as JSON with the error code and text
>     }
>     else {
>       // ...  must I render here?? as JSON with the OK
>     }
>   });
>
>   // I would like to return something to the controller!! from here, or 
> from the callback
> }
>
>
>
> And for the controller… if I use $self->render_later… do the controller 
> know that the render will occur in the model?
>
> Any suggestions? All the non-blocking tutorials and docs use just a "say" 
> or a render, but it seems that I want to "break" the callback. Maybe I need 
> another structure.
>
> Thank you in advance!
> Nacho B.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to