Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a route and resource in New Router API?

I am trying to understand the difference between a Route and a Resource. The way I understand Resource helps to set sub paths of a Route object to another Route Object. But its unclear when i think of default name mapping happening for paths as well.

like image 812
thecodejack Avatar asked Feb 20 '13 06:02

thecodejack


People also ask

What is a route in an API?

Routes direct incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource path—for example, GET /pets . You can define specific HTTP methods for your route. Or, you can use the ANY method to match all methods that you haven't defined for a resource.

What is a resource route?

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. A single call to resources can declare all of the necessary routes for your index , show , new , edit , create , update , and destroy actions.

What is difference between route and router?

In a WAN, routers serve as connection points between sub-networks, more generally referred to as nodes. Routing between sub-networks is guided by a routing table maintained in each end-system. The routing table points to the next device along a route for a packet to take in order to reach a given address.

What is the difference between a route and an endpoint?

Routes vs Endpoints Endpoints perform a specific function, taking some number of parameters and return data to the client. A route is the “name” you use to access endpoints, used in the URL. A route can have multiple endpoints associated with it, and which is used depends on the HTTP verb.


1 Answers

Please Note that from 1.11.0 onwards, this.route is only used instead of this.resource. Source: http://guides.emberjs.com/v1.11.0/routing/defining-your-routes/*

Have a look at this post for a detailed explanation.

This is a rough summary of this post (i have modified a bit):

Ever since the change to resource and route a lot of people are confused about the meaning of the two and how they affect naming. Here’s the difference:

  • resource - a thing (a model)
  • route - something to do with the thing

So this means a router using a route and resource might look like this:

App.Router.map(function() {   this.resource("posts", { path: "/" }, function() {     this.route("new", { path: "/new" });   });   this.route("another", { path: "/another" }); }); 

This would result in the following routes being created/used:

  • PostsRoute, PostsController, PostsView
  • PostsIndexRoute, PostsIndexController, PostsIndexView
  • PostsNewRoute, PostsNewController, PostsNewView
  • AnotherRoute, AnotherController, AnotherView

As we see from this example, resource effect the naming of the Controllers,Routes and Views being used/created (The "new" route is treated as subordinate to "posts" resource). Cite from the original source (i modified it, because it was irritating as Patrick M correctly pointed out in the comments):

This means whenever you create a resource it will create a brand new namespace. That namespace is named after the resource and all of the child routes will be inserted into it.

Update: more complex example with nested resources

Consider the following more complex example with multiple nested resources:

App.Router.map(function() {   this.resource("posts", { path: "/" }, function() {     this.route("new", { path: "/new" });     this.resource("comments", { path: "/comments" }, function() {       this.route("new", { path: "/new" });     });   });   this.route("another", { path: "/another" }); }); 

In this case the resource comments creates a brand new namespace. This means the resulting routes in this case will be the following. As you can see the Route, Controller and View for the comments resource are not prefixed with the name of the parent route. That means nesting a resource within another resource resets the namespace (= creates a new namespace).

  • PostsRoute, PostsController, PostsView
  • PostsIndexRoute, PostsIndexController, PostsIndexView
  • PostsNewRoute, PostsNewController, PostsNewView
  • CommentsRoute, CommentsController, CommentsView
  • CommentsNewRoute, CommentsNewController, CommentsNewView
  • AnotherRoute, AnotherController, AnotherView

This behaviour is also explained in the Ember Docs.

like image 119
mavilein Avatar answered Oct 03 '22 14:10

mavilein