界面上的有限状态机(四)

2020-12-25  本文已影响0人  寻找无名的特质

前面介绍了有限状态机的状态和转换,具体界面响应是在状态转换时进行的,比如如果用户按了按钮,某些控件会不可见,这些动作需要在状态转换时的事件响应中完成,在状态转换时,会有有五种事件发生,按顺序如下:

还有两种简化的写法:

这些名称需要使用javascript的驼峰命名方式,不管状态和转移如何定义,下面是例子:

  var fsm = new StateMachine({
    transitions: [
      { name: 'do-with-dash',       from: 'has-dash',        to: 'has_underscore'   },
      { name: 'do_with_underscore', from: 'has_underscore',  to: 'alreadyCamelized' },
      { name: 'doAlreadyCamelized', from: 'alreadyCamelize', to: 'has-dash'         }
    ],
    methods: {
      onBeforeDoWithDash:         function() { /* ... */ },
      onBeforeDoWithUnderscore:   function() { /* ... */ },
      onBeforeDoAlreadyCamelized: function() { /* ... */ },
      onLeaveHasDash:             function() { /* ... */ },
      onLeaveHasUnderscore:       function() { /* ... */ },
      onLeaveAlreadyCamelized:    function() { /* ... */ },
      onEnterHasDash:             function() { /* ... */ },
      onEnterHasUnderscore:       function() { /* ... */ },
      onEnterAlreadyCamelized:    function() { /* ... */ },
      onAfterDoWithDash:          function() { /* ... */ },
      onAfterDoWithUnderscore:    function() { /* ... */ },
      onAfterDoAlreadyCamelized:  function() { /* ... */ }
    }
  });
上一篇下一篇

猜你喜欢

热点阅读