Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed Feature Addition: RespondTo Annotation #10

Closed
wants to merge 5 commits into from

Conversation

mmurray
Copy link
Contributor

@mmurray mmurray commented Oct 17, 2010

Hi there,

I have a proposed feature addition for Play. I created an annotation in play.mvc called RespondTo. The annotation is used to specify which formats a Controller or Action will respond to automatically.

Here is a quick example of usage:
@RespondTo({"json","xml"})
public class Articles extends Controller {

  public static void index() {
    List<Article> articles = Article.all().fetch();
    render(articles);
  }

  public static void show(Long id) {
    Article article = Article.findById(id);
    render(article);
  }

}

When a request comes in for Articles/show (or Articles/index) and the format requested is either "json" or "xml", then renderJSON or renderXml will be called on the args if there is no show.json or show.xml view. This keeps our action methods DRY and concise because we can avoid copying this all over the place:
if(request.format.equals("json")){
renderJSON(myModel);
}else if(request.format.equals("xml")){
renderXml(myModel);
}

And we don't have to create and maintain a bunch of .json and .xml versions of our views (If the views do exist they will take precedence though).

Also, RespondTo annotations defined at the action level will take precedence over RespondTo annotations on the controller.

So in this example:
@RespondTo({"json","xml"})
public class Articles extends Controller {

  public static void index() {
    List<Article> articles = Article.all().fetch();
    render(articles);
  }

  @RespondTo("xml")
  public static void show(Long id) {
    Article article = Article.findById(id);
    render(article);
  }

}

Articles/index will respond to json or xml automatically, while Articles/show will only respond to xml automatically (if json was requested and there is no .json view then it will just raise a TemplateNotFoundException like normal)

So yeah, that's what I've been playing around with on my fork. It's heavily inspired by Ruby on Rail's new respond_to decorator, and it helps me maintain flexible APIs while keeping my controllers concise and DRY.

And I just want to add that I'm absolutely loving Play so far, you guys have done a really great job with this framework. I'm trying to convince my team at work to migrate our website (which is currently PHP/Java) over to Play.

@mmurray
Copy link
Contributor Author

mmurray commented Oct 17, 2010

Doesn't look like this will work as a Plugin, unfortunately the PlayPlugin.loadTemplate only gets a chance to run if the template VirtualFile exists. There is no way to prevent the TemplateNotFoundException without modifying TemplateLoader.load(), and there is no way to check if a template exists without duplicating the logic in TemplateLoader.load().

@mmurray
Copy link
Contributor Author

mmurray commented Oct 18, 2010

Closing this because I'm going to try to implement it as a module.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant