Routes javascript export for ajax-oriented application

67 views
Skip to first unread message

Gaetan Renaudeau

unread,
Nov 21, 2009, 4:14:37 AM11/21/09
to play-framework
Currently, When you change some routes path and method,
you have to review all javascript codes which refer to these routes.
But generally, the Controller.action name less change.

So I made a tag to export routes and a javascript class
for AJAX oriented application.

I made a first version of the $.ajax function to simplify it.

For instance :
# Routes
POST /contact/{contactid}/rename Contact.rename

// In Contact.java :
public static void rename (Long contactid, String newname);

In javascript :

var contactid;
var newname;

/* With jQuery we must write : */

$.ajax({
type: "POST",
url: "/contact/"+contactid+"/rename",
data: {newname:newname},
dataType: "json",
success: function(data) {
// do something
},
error: function(data) {
// do something
}
});

/* Now with my system, you write simply : */
var myLibraryPath = "application.routes"; // for example
myLibraryPath.ajax("Contact.rename", {contactid:contactid,
newname:newname});

So you don't have to worry about the path and the method of the route
but only the controller.action name and the data to send (so you only
have to know the java method signature).

You don't see the callback function because I formalism them :
If success we trigger an event like this : $().trigger
("Contact.rename", data);
If error : "Contact.rename.error" is triggered.

But maybe I can manage a third arguments to override jquery ajax
options you want (except type, url, data and dataType).


I have some others idea to more simplify an ajax-oriented application.
Maybe we can create a module to regroup them later ?

For the moment I created a branch to share my routes javascript
exporter : https://code.launchpad.net/~gre/play/routes_js_export

It's so experimental for the moment, but I will improve it in the
future.

Best regards,
Gaetan

Gaetan Renaudeau

unread,
Nov 21, 2009, 4:25:40 AM11/21/09
to play-framework
I forget to say something.

In the future we can imagine some options to the routes tag to filter
the export by controller (because we don't want to export the Admin.*
actions to users).
For the moment, export all routes it's not too secure and is a gold
mine for hackers.

Regards,
Gaetan

Guillaume Bort

unread,
Nov 21, 2009, 12:56:39 PM11/21/09
to play-fr...@googlegroups.com
Nice,

But are you sure that if follows the same rules that the internal Router ?
> --
>
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=.
>
>
>

Gaetan Renaudeau

unread,
Nov 21, 2009, 1:35:33 PM11/21/09
to play-framework
If you refer to the rules about url compute, my version replace the
variable value without verify the regexp (but the server side will
verify this).

For exemple :

/contact/{<[0-9]>contactid}/rename
is compute like
/contact/{contactid}/rename

But collision aren't possible because, contrary to server-side, the
url is selected from the controller.action name (and this is unique).

In fact, i don't know if I must interrupt the request before sending
it or let the error for the server-side.
Maybe it's better to interrupt it on client-side but it's so worst for
debugging if the ajax request is never send.

For the moment, my ajax function will return true if there is a
problem to compute url (an absent args) or if the specified
controller.action name isn't found and false else.

Maybe I must throw an Exception in javascript when these error
occured ?


Regards,
Gaetan
Reply all
Reply to author
Forward
0 new messages