Emberjs Complex Routing

懵懂的女人 提交于 2019-12-11 14:57:16

问题


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:


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



来源:https://stackoverflow.com/questions/25817621/emberjs-complex-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!