如何在Sproutcore中离开州?

时间:2012-08-15 09:59:40

标签: sproutcore

如何在Sproutcore中留下一个州?实际上,我正在寻找相当于:

MyApp.statechart.gotoState('StateA');

还有另一种方法可以手动离开状态而不是通过substatesAreConcurrent: NO属性控制状态吗?例如:StateA和StateB是并发的。 StateB有两个嵌套的子状态:StateBa和StateBb。当我从StateBa切换到StateBb时,我想离开StateA。这怎么可能?

提前谢谢!

1 个答案:

答案 0 :(得分:3)

给出状态图:

App.statechart = SC.Statechart.create({

   substatesAreConcurrent: YES,

   stateA: SC.State.extend({
   }),

   stateB: SC.State.extend({

      initialSubstate: 'stateBa'

      stateBa: SC.State.extend({
      }),

      stateBb: SC.State.extend({
      })

   })

});

您的应用程序同时位于stateA和stateB内。为了在从stateBa> stateBb转换时从stateA转换到某个其他状态,我们必须如下增强stateA:

   stateA: SC.State.extend({

      initialSubstate: 'stateAa',

      stateAa: SC.State.extend({
      }),

      stateAb: SC.State.extend({
      })

   }),

实现这一目标的惯用方法是在两个州处理相同的事件,比如说“改变”,如下所示:

App.statechart = SC.Statechart.create({

   substatesAreConcurrent: YES,

   stateA: SC.State.extend({

      initialSubstate: 'stateAa',

      stateAa: SC.State.extend({

         change: function() {
            this.gotoState('stateAb');
         }

      }),

      stateAb: SC.State.extend({
      })

   }),

   stateB: SC.State.extend({

      initialSubstate: 'stateBa'

      stateBa: SC.State.extend({

         change: function() {
            this.gotoState('stateBb');
         }

      }),

      stateBb: SC.State.extend({
      })

   })

});

这样,发送“change”事件会导致两个并发状态的状态转换。但是,如果您有兴趣保留并发性,那么您可以像这样增强状态图:

App.statechart = SC.Statechart.create({

   stateSuperA: SC.State.extend({
      substatesAreConcurrent: YES,

      stateA: SC.State.extend({

         initialSubstate: 'stateAa',

         stateAa: SC.State.extend({

            change: function() {
               this.gotoState('stateAb');
            }

         }),

         stateAb: SC.State.extend({

            join: function() {
               this.gotoState('stateSuperB');
            }

         })

      }),

      stateB: SC.State.extend({

         initialSubstate: 'stateBa'

         stateBa: SC.State.extend({

            change: function() {
               this.gotoState('stateBb');
            }

         }),

         stateBb: SC.State.extend({
         })

      })

   }),

   stateSuperB: SC.State.extend({
   })

});

收到上面的“join”事件后,您的应用程序将不再是并发子状态,而是仅在stateSuperB中。