Emberjs Complex Routing

2019-09-01 14:37发布

In my application there is a client side for members to select bottles and an admin section to manage members, bottles, lockers etc. that both use the same models so I figured that my my router should look like this:

App.Router.map ->
  @.route "signin" #Restuarant signs in to the application
  @.resource "member", -> 
    @.route "signin" #Members sign in to see their bottles
    @.resource "member", path: ":member_id", ->
      @.resource "bottles", ->
        @.route "select"
      @.resource "transcations", ->
        @.route "create"
        @.resource "transcation", path: ":transcation_id", ->
  @.resource "admin", ->        
    @.route "signin" #Restaurant signs in to manage
    @.resource "members", ->
      @.route "create"
      @.resource "member", path: ":member_id", ->
        @.resource "bottles", ->
          @.route "create"
          @.resource "bottle", path: ":bottle_id", ->
            @.route "edit"
        @.resource "transcations", ->
          @.resource "transcation", path: ":transcation_id", ->
    @.resource "lockers", ->
      @.route "create"
      @.resource "locker", path: ":locker_id", ->
      @.resource "lockertype", path: "types", ->
        @.resource "lockertype", path: ":locker_type_id", ->
          @.route "edit"  
        @.route "create"  

I understand that the resources under admin will replace the resources for the members that have the same name.

What would be the best pattern to solve this?

My current solution would be to namespace (kinda?) the resources like this:

    @.resource "member", -> 
      @.route "m.signin" #Members sign in to see their bottles
      @.resource "m.member", path: ":member_id", ->
        @.resource "m.bottles", ->
          @.route "select"
        @.resource "m.transcations", ->
          @.route "create"
          @.resource "m.transcation", path: ":transcation_id", ->

Is there a better way? Is this way terrible? I've been looking up EMBER.NAMESPACE but I don't understand how I would use it.

Relevant discussions on github:

https://github.com/emberjs/ember.js/issues/683 https://github.com/emberjs/ember.js/pull/1925

1条回答
劳资没心,怎么记你
2楼-- · 2019-09-01 15:15

If you declare nested resource, it's name must contain parent resource's name ended with dot: 'members.member', 'members.member.bottles', etc. Names of tail routes are simpler ('signin' insted of 'member.signin'). You can observe all application's routes with help of chrome extension (Ember Inspector).

Update from 14 Sep: I used to an old versions of Ember and somehow missed an update in routing system. There is no need for this.resource call in the last Ember version (1.7.0). This means you can use only this.route method, which is easer and more clear: http://emberjs.jsbin.com/bukogojuzeni/1/edit?html,js,output

查看更多
登录 后发表回答