Discussion:
[Mojolicious] Return mojo::pg result from the model to the controller to be rendered
Nacho B
2018-09-24 11:00:41 UTC
Permalink
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
sri
2018-09-24 13:58:52 UTC
Permalink
Just return a promise. And use ->update_p instead of ->update.

--
sebastian
--
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Scott Wiersdorf
2018-09-24 16:29:29 UTC
Permalink
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
Post by Nacho B
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 {
$self->pg->db->update('party_t', $party, {id => $id} => sub {
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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Nacho B
2018-09-24 18:43:55 UTC
Permalink
Thank you, Sebastian, and Scott for the expanded answer.

I've refactored all the queries in a snap!

Promises A+ => Mojolicious A++++++ :-)

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 mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Loading...