Skip to content

define routing to a class method? #382

Answered by gi0baro
erhuabushuo asked this question in Q&A
Discussion options

You must be logged in to vote

@erhuabushuo Emmett doesn't provide any ready-to-go facilities for that, mainly because in Emmett a class based view is an anti-pattern, secondly to prevent wasting performance in creating objects during request flow.

You might want to implement your own AppModule class for such logic, somewhat like the REST extension does:

from emmett import AppModule

class ViewModule(AppModule):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._define_routes()

    def _define_routes(self):
        for method in ["get", "post", "put", "delete"]:
            if handler := getattr(self, method, None):
                self.route(methods=[method], name=method)(h…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by erhuabushuo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #381 on September 21, 2021 06:30.