Created
June 24, 2010 10:34
-
-
Save mikekelly/451295 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (/app/resources/useraccount.js) | |
Resource({ | |
respond_to: ["html", "xml", "json"], | |
http: { | |
GET: function() { | |
user = User.find(params.user); | |
respond_with(user); | |
}, | |
PUT: function() { | |
user = User.update(request.body); | |
respond_with(user); | |
} | |
} | |
}); | |
/* | |
* Resource function creates resource that is a jsgi component, | |
* these jsgi resources take a request and map it to the appropriate | |
* method handler | |
* | |
* respond_to: An array listing negotiable representations of the resource | |
* respond_to/respond_with pattern nicked from rails3 :) | |
* | |
* http: This is the most commonly used protocol, but the framework is | |
* agnostic about what protocols you use. | |
* | |
* In above example POST, DELETE, OPTIONS not defined so | |
* should respond with 405 automagically | |
* | |
*/ | |
// (/app/routes.js) | |
router.route({ | |
name: "user_account", | |
match: "/user/:user", | |
resource: resources.useraccount, | |
URI: function(args) { | |
return "/user/" + args.user.id; | |
} | |
}); | |
/* | |
* | |
* route function creates route and adds it to the router | |
* the router is jsgi middleware which matches the request | |
* and dispatches it to the correct resource | |
* | |
* | |
* name: Routes can/should be named so they can be reffered back to | |
* e.g. Route.user_account | |
* | |
* match: The URI pattern to match against | |
* | |
* resource: The Resource object that this route leads to | |
* | |
* URI: The URI function is passed an args object containing relevant info | |
* that can be used to generate the URI (e.g. a user object) | |
* | |
* Examples: | |
* Route.user_account.URI(a_user_object) => "/user/fred123" | |
* Route.all_users.URI() => "/users" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment