在SproutCore中直接路由到子状态时,如何设置控制器选择? [英] How do you set up controller selections when routing directly to a child state in SproutCore?

查看:101
本文介绍了在SproutCore中直接路由到子状态时,如何设置控制器选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个通往应用程序深层状态的路由,那么即使我要直接进入内部状态,如何确保已完成正确的控制器设置?

If I have a route to a state deep in the application, how do I ensure that the proper controller set up has been done although I'm going to enter the inner state directly?

例如,

  • 状态A
    • 状态a,代表路线:"a"
    • 状态B
      • 状态b,代表路线:"a/b"
      • 州C
        • 状态c,代表路线:'a/b/c'
        • 州D
          • 状态d,代表路线:'a/b/c/d
          • 州E
          • state A
            • state a, representRoute: 'a'
            • state B
              • state b, representRoute: 'a/b'
              • state C
                • state c, representRoute: 'a/b/c'
                • state D
                  • state d, representRoute: 'a/b/c/d
                  • state E

                  如您所见,您可以直接路由到状态'a','b','c'或'd',但看不到的是通常您可以通过在其中选择一个项来进入这些状态控制器,然后将触发状态转换到更深的状态.然后的问题是,直接进入状态d时,没有设置控制器的选择.

                  As you can see you can route directly to states 'a', 'b', 'c' or 'd', but what you can't see is that normally you would go between these states by selecting an item in a controller, which would then trigger a state transition to the deeper state. The problem then is that when you go directly to state 'd' none of your controllers' selections are set up.

                  到目前为止,我已经在状态'a'中使用enterStateByRoute来设置第一个控制器的选择,然后不得不在状态'b'中使用enterStateByRoute来选择第一和第二个控制器,依此类推.一直到状态'd'的enterStateByRoute一直进行每个控制器的选择.这非常浪费,因为我最终在每个enterStateByRoute中重复相同的代码.

                  So far I've used enterStateByRoute in state 'a' to set the selection of the first controller and then had to use enterStateByRoute in state 'b' to do the selection of the first and second controllers, etc. all the way to enterStateByRoute in state 'd' to do the selection of each controller all the way along. This is quite wasteful, because I end up repeating the same code in each enterStateByRoute.

                  设置控制器选择以匹配直接路由状态的最佳方法是什么?

                  What is the best way to set the controller selection to match the directly routed state?

                  推荐答案

                  当我意识到在路由时在链中所有父状态上都调用enterStateByRoute时,我可以极大地改善这种情况.这意味着,如果状态"c"与路由匹配,则将进入状态"A",然后进入状态"B"和状态"C",最后才进入状态"c".我以前没有意识到的是,进入这些状态时,每个状态都传递了SC.StateRouteHandlerContext对象,允许您检查enterState中的上下文或在任何状态下实现enterStateByRoute.

                  I was able to improve the situation greatly once I realized that enterStateByRoute is called on all parent states in the chain when routing. This means that if state 'c' matches the route, state 'A' will be entered, followed by state 'B' and state 'C' before finally entering state 'c' last. What I didn't realize before was that each of these states is passed the SC.StateRouteHandlerContext object as it is entered allowing you to either check the context in enterState or implement enterStateByRoute in any of the states.

                  然后我的解决方案是将enterStateByRoute添加到状态'A'来设置第一个控制器,将enterStateByRoute添加到状态'B'来设置第二个控制器,依此类推.例如,以这种方式,过去的任何状态状态'A'被保证具有第一个控制器选择集,而我在链中没有任何重复的代码.

                  My solution then was to add enterStateByRoute to state 'A' to set the first controller, add enterStateByRoute to state 'B' to set the second controller, etc. For example, in this way, any state past state 'A' is guaranteed to have the first controller selection set and I don't have any duplicated code down the chain.

                  例如,

                  // … 
                  state_A: SC.State.extend({
                    initialSubstate: 'state_a',
                  
                    enterStateByRoute: function (context) {
                      // select object on controller 1 since we are routing
                    },
                  
                    state_a: SC.State.extend({
                      representRoute: 'a',
                  
                      enterStateByRoute: function (context) {
                        // do setup for state 'a' specific to routing
                      }
                    }),
                  
                    state_B: SC.State.extend({
                      initialSubstate: 'state_b',
                  
                      enterStateByRoute: function (context) {
                        // select object on controller 2 since we are routing
                      },
                  
                      state_b: SC.State.extend({
                  
                        enterStateByRoute: function (context) {
                          // do set up for state 'b' specific to routing
                        },
                  // …
                  

                  我遇到的唯一问题是,因为我已经将所有控制器绑定在一起,所以选择更改不会立即传播,因此我将选择处于第一种状态的控制器上的对象,进入下一个状态并发现绑定控制器的内容尚未更新.

                  The only problem I encountered was that because I had bound all my controllers together, the selection change doesn't propagate immediately and so I would select an object on a controller in the first state, enter the next state and find that the bound controllers' content would not yet have updated.

                  因此,我本可以等待绑定刷新,方法是在enterStateByRoute中返回一个SC.Async对象,并在运行循环结束时使用this.invokeLast(function () { this.resumeGotoState(); })进入下一个状态,但是我采取了一种声明性的方法,只需输入/退出相应状态,即可设置/取消每个控制器的内容.

                  So I could have waited for bindings to flush by returning an SC.Async object in enterStateByRoute and used this.invokeLast(function () { this.resumeGotoState(); }) to go to the next state at the end of the run loop, but instead I took a declarative approach and simply set/unset each controller's content as I enter/exit the appropriate state.

                  这篇关于在SproutCore中直接路由到子状态时,如何设置控制器选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆