Skip to content

Loading…

[#1103] Add a jsRoute tag #343

Merged
merged 1 commit into from

2 participants

@julienrf

Patch for #1103

@pepite pepite merged commit 02d4176 into playframework:master
@pepite
Play Framework member

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 12, 2011
  1. @julienrf

    [#1103] Add a jsRoute tag

    julienrf committed
View
20 documentation/manual/tags.textile
@@ -352,6 +352,26 @@ bc. <script type="text/javascript">
</script>
+h2. <a name="jsroute">jsRoute</a>
+
+The @#{jsRoute /}@ tag is similar to the @#{jsAction /}@ tag, it returns an object containing both the function which consctructs the URL based on the server action, and the corresponding HTTP method (GET, POST, etc.).
+
+Example:
+
+bc. PUT /users/{id} Users.update
+
+Then, in a template:
+
+bc. <script type="text/javascript">
+ var updateUserRoute = #{jsRoute @Users.update(':id') /}
+ $.ajax({
+ url: updateUserRoute.url({id: userId}),
+ type: updateUserRoute.method,
+ data: 'user.name=Guillaume'
+ });
+</script>
+
+
h2. <a name="list">list</a>
Iterates over an object collection.
View
16 framework/src/play/templates/FastTags.java
@@ -59,6 +59,22 @@ public static void _jsAction(Map<?, ?> args, Closure body, PrintWriter out, Exec
out.println("function(options) {var pattern = '" + args.get("arg").toString().replace("&amp;", "&") + "'; for(key in options) { pattern = pattern.replace(':'+key, options[key]); } return pattern }");
}
+ public static void _jsRoute(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
+ final Object arg = args.get("arg");
+ if (!(arg instanceof ActionDefinition)) {
+ throw new TemplateExecutionException(template.template, fromLine, "Wrong parameter type, try #{jsRoute @Application.index() /}", new TagInternalException("Wrong parameter type"));
+ }
+ final ActionDefinition action = (ActionDefinition)arg;
+ out.print("{");
+ if (action.args.isEmpty()) {
+ out.print("url: function() { return '" + action.url.replace("&amp;", "&") + "'; },");
+ } else {
+ out.print("url: function(args) { var pattern = '" + action.url.replace("&amp;", "&") + "'; for (var key in args) { pattern = pattern.replace(':'+key, args[key]); } return pattern; },");
+ }
+ out.print("method: '" + action.method + "'");
+ out.print("}");
+ }
+
public static void _authenticityToken(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
out.println("<input type=\"hidden\" name=\"authenticityToken\" value=\"" + Session.current().getAuthenticityToken() + "\">");
}
View
8 samples-and-tests/just-test-cases/app/controllers/Application.java
@@ -166,6 +166,14 @@ public static void reverserouting3() {
renderText(def);
}
+ public static void jsRoute() {
+ render();
+ }
+
+ public static void jsRouteError() {
+ render();
+ }
+
public static void mail() {
notifiers.Welcome.welcome();
renderText("OK");
View
31 samples-and-tests/just-test-cases/app/views/Application/jsRoute.html
@@ -0,0 +1,31 @@
+#{extends 'main.html' /}
+
+<script type="text/javascript">
+ function set(eltId, content) {
+ var elt = document.getElementById(eltId);
+ if (elt !== null) {
+ elt.textContent = content;
+ }
+ }
+</script>
+
+<div id="a-url"></div>
+<div id="a-method"></div>
+
+<div id="b-url"></div>
+<div id="b-method"></div>
+
+<div id="c"></div>
+
+<script type="text/javascript">
+ var a = #{jsRoute @Application.index() /};
+ set('a-url', a.url());
+ set('a-method', a.method);
+
+ var b = #{jsRoute @Application.hello() /};
+ set('b-url', b.url());
+ set('b-method', b.method);
+
+ var c = #{jsRoute @Rest.postOrPut(':id') /};
+ set('c', c.url({id: 'foo'}));
+</script>
View
3 samples-and-tests/just-test-cases/app/views/Application/jsRouteError.html
@@ -0,0 +1,3 @@
+<script type="text/javascript">
+ #{jsRoute 'toto' /}
+</script>
View
11 samples-and-tests/just-test-cases/test/routing.test.html
@@ -150,4 +150,15 @@
open('/client')
assertTextPresent('localhost')
+
+ // jsRoute tag
+ open('@{Application.jsRoute()}')
+ assertText('a-url', '/')
+ assertText('a-method', 'GET')
+ assertText('b-url', '/sayHello')
+ assertText('b-method', 'PUT')
+ assertText('c', '/ressource/foo')
+
+ open('@{Application.jsRouteError()}')
+ assertTextPresent('Wrong parameter type')
#{/selenium}
Something went wrong with that request. Please try again.