1

我正在构建一个具有持久视图的简单应用程序。这种观点一直存在。视图取决于一些数据。以不同的状态进入应用程序应确保视图与其数据一起存在。我认为最好的方法是创建一个调用connectOulet连接数据和视图的初始状态。这样,所有后续状态都必须通过这一状态进行转换。我不知道如何让路由器在连接后自动转换到下一个状态。

这是我的路由器(这在 ATM 上不起作用)。看评论。

Inbox.Router = Ember.Router.extend
  enableLogging: true
  location: "history"
  initialState: "bootUp"

  bootUp: Ember.Route.extend
    root: Ember.Route.extend
      route: '/'
      # All states need to transition through this code
      # How can I transition to root state after this code executes?
      connectOutlets: (router, context) ->
        router.get('applicationController').connectOutlet
          outletName: "list"
          name: "messages"
          context: Inbox.store.findAll(Inbox.Message)

  root: Ember.Route.extend
    showMessage: Ember.Route.transitionTo('root.showingMessage')

    initialState: 'dashboard'

    dashboard: Ember.Route.extend
      route: '/dashboard'

    # Entering this state needs the messages view loaded
    # with data
    showingMessage: Ember.Route.extend
      route: '/:id'
      connectOutlets: (router, context) ->
        router.get('applicationController').connectOutlet
          outletName: "details"
          name: "message"
          context: context
      serialize: (router, context) ->
        id: context.get('id')
      deserialize: (router, urlParams) ->
        Inbox.store.find Inbox.Message, urlParams.id

用 FSM 建模这种行为对我来说似乎是正确的。这是做这种事情的正确方法还是有更好的方法?

4

2 回答 2

1

解决方案是创建一个状态根状态,事物通过该状态进行转换。root状态本身不可路由。所以我们必须为此创建一个子状态。这是我的路由器。

# Assume users enters a /

Inbox.Router = Ember.Router.extend
  enableLogging: true
  location: "history"

  # Root application state
  root: Ember.Route.extend

    # This is the state that the others must transition.
    # It's route is "/". It has a nested state called "home"
    # which route is "/". Home's route is relative to "/". 
    # So when a user enters at "/" the "home" state is selected.
    # The router transitions into "index" then "home".
    # When the user enters at "/:id" the user transitions through "index"
    # since "/:id" is a subroute of "/".
    # 
    # This is explained in the router's nested state documentation.
    index: Ember.Route.extend
      route: '/'
      connectOutlets: (router, context) ->
        router.get('applicationController').connectOutlet
          outletName: "list"
          name: "messages"
          context: Inbox.store.findAll(Inbox.Message)
        router.get('applicationController').connectOutlet
          outletName: "details"
          name: "message"
        router.get('applicationController').connectOutlet
          outletName: "filters"
          controller: router.messagesController
          viewClass: Inbox.FiltersView

      showMessage: Ember.Route.transitionTo('showingMessage')

      home: Ember.Route.extend
        route: '/'

      showingMessage: Ember.Route.extend
        route: '/:id'
        connectOutlets: (router, context) ->
          router.get('messageController').set('content', context)
        serialize: (router, context) ->
          id: context.get('id')
        deserialize: (router, urlParams) ->
          Inbox.store.find Inbox.Message, urlParams.id
于 2012-11-14T22:49:48.793 回答
0

You should just be able to call connectOutlets in the root Route. I don't think you can create route/states above or before root like your example.

root: Ember.Route.extend
showMessage: Ember.Route.transitionTo('root.showingMessage')
initialState: 'dashboard'

connectOutlets: (router) ->
  router.get('applicationController').connectOutlet
    outletName: "list"
    name: "messages"
    context: Inbox.store.findAll(Inbox.Message)

dashboard: Ember.Route.extend
  route: '/dashboard'

# Entering this state needs the messages view loaded
# with data
showingMessage: Ember.Route.extend
  route: '/:id'
  connectOutlets: (router, context) ->
    router.get('applicationController').connectOutlet
      outletName: "details"
      name: "message"
      context: context
  serialize: (router, context) ->
    id: context.get('id')
  deserialize: (router, urlParams) ->
    Inbox.store.find Inbox.Message, urlParams.id
于 2012-11-08T00:08:31.450 回答