Skip to content

Commit

Permalink
[playframework#1103] Add a jsRoute tag
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrf committed Sep 12, 2011
1 parent f74ef27 commit fee1dbe
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
20 changes: 20 additions & 0 deletions documentation/manual/tags.textile
Expand Up @@ -352,6 +352,26 @@ bc. <script type="text/javascript">
</script> </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> h2. <a name="list">list</a>


Iterates over an object collection. Iterates over an object collection.
Expand Down
16 changes: 16 additions & 0 deletions framework/src/play/templates/FastTags.java
Expand Up @@ -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 }"); 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) { 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() + "\">"); out.println("<input type=\"hidden\" name=\"authenticityToken\" value=\"" + Session.current().getAuthenticityToken() + "\">");
} }
Expand Down
Expand Up @@ -166,6 +166,14 @@ public static void reverserouting3() {
renderText(def); renderText(def);
} }


public static void jsRoute() {
render();
}

public static void jsRouteError() {
render();
}

public static void mail() { public static void mail() {
notifiers.Welcome.welcome(); notifiers.Welcome.welcome();
renderText("OK"); renderText("OK");
Expand Down
@@ -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>
@@ -0,0 +1,3 @@
<script type="text/javascript">
#{jsRoute 'toto' /}
</script>
11 changes: 11 additions & 0 deletions samples-and-tests/just-test-cases/test/routing.test.html
Expand Up @@ -150,4 +150,15 @@
open('/client') open('/client')
assertTextPresent('localhost') 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} #{/selenium}

0 comments on commit fee1dbe

Please sign in to comment.