繁体   English   中英

高级路由和单个对象定义?

[英]Advanced routing and single object definition?

我正在尝试简化路线。 现在,我仍然需要在扩展router的类内使用引用,但是由于我使用eval实例化引用,因此每个route方法都是相同的

目前,在我的应用程序内部:

ROUTES =
  'action1/:page': 'action1'
  'action2/:page': 'action2'
  '*path': 'default' # Default routes to action1

# Start Backbone history.
App.Router = new Routes(
  routes: ROUTES
)
App.Router.setDefaultRoute 'action1'
Backbone.history.start()

和Router类:

class window.Routes extends Backbone.Marionette.AppRouter

  constructor: (options) ->

    @on 'all', @storeRoute
    @history = []
    super options

  @trace initialize: () ->

    debug.info 'Routes: ' + JSON.stringify @routes

  @trace setDefaultRoute: (route) ->

    App.Data.defaultRoute = route

  @trace getDefaultRoute: () ->

    App.Data.defaultRoute

  @trace storeRoute: () ->

    @history.push Backbone.history.fragment

  @trace getPrevious: () ->

    if @history.length > 1
      @history[@history.length - 1] 

  @trace getPreviousRoute: () ->

    @getPrevious().split('/')[0]

  @trace getPreviousPage: () ->

    @getPrevious().split('/')[1]

  @trace getFormattedWindowHash: () ->

    hash = window.location.hash
    hash = hash.substring(1, hash.length) 

    hash

  @trace getCurrentRoute: () ->

    current = @getFormattedWindowHash().split('/')[0]

    # Allow us to not specify 'route = <route>' inside every routing method.
    if !current
      current = @getDefaultRoute()
    else
      current = current

    current = current.charAt(0).toUpperCase() + current.slice(1)    

  @trace getCurrentPage: () ->

    @getFormattedWindowHash().split('/')[1]

  # Everytime a page is loaded, default data is cleared by instantiating new 
  # Views, Events, UI, State, and Error classes.
  @trace setupPage: (page) ->

    route = @getCurrentRoute()

    debug.info 'current route: ' + route

    event = 'App.Pages.Events.' + route + ' = new App.Events.' + route + '()'
    debug.info 'eval event: ' + event

    eval event

    # The magic which routes to the page.
    goTo = 'App.Pages.Events.' + route + '.router(parseInt(' + page + '))'
    debug.info 'goTo: ' + goTo

    eval goTo

  # Place routing actions below.

  @trace action1: (page) ->

    @setupPage page

  @trace action2: (page) ->

    @setupPage page

  # Default route.
  @trace default: () ->

    # TODO make this settable?
    @action1 1

我想要做的是能够删除这3种方法:

 @trace action1: (page) ->

    @setupPage page

  @trace action2: (page) ->

    @setupPage page

  # Default route.
  @trace default: () ->

    # TODO make this settable?
    @action1 1

我该怎么做?

正在运行: http//franklovecchio-playback.herokuapp.com/?log = true

完整代码: https//github.com/franklovecchio/playback

您可以使用两个路由处理程序:

ROUTES =
  'action1/:page': 'page'
  'action2/:page': 'page'
  '*path': 'def'

接着:

page: (page) ->
  @setupPage(parseInt(page, 10))
def: ->
  @setupPage(1)

splat路由( *path )会将路由的匹配部分发送到处理程序,因此您不能仅使用一个路由处理程序而不尝试解释splat匹配的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM