zoukankan      html  css  js  c++  java
  • 有限状态机

     var ctrl1 = {
          activate: function() {
               console.log("ctrl1-active");
          },
          deactivate: function() {
               console.log("ctrl1-deactive");
          }
    };
     var ctrl2 = {
          activate: function() {
               console.log("ctrl2-active");
          },
          deactivate: function() {
               console.log("ctrl2-deactive");
          }
    };
     var ctrl3 = {
          activate: function() {
               console.log("ctrl3-active");
          },
          deactivate: function() {
               console.log("ctrl3-deactive");
          }
    };

    ctrl1,ctrl2,ctrl3表示控制器,当我们要在多个控制器之间切换视图时,就需要引入有限状态机技术.

    function Statemachine() {};
    
    var extend = (function() {
        var F = function() {};
        return function(C, P) {
            F.prototype = P.prototype;
            C.prototype = new F();
            C.uper = P;
            C.prototype.constructor = C;
        };
    })();
    
    // EventProxy is an Event.js
    extend(Statemachine, EventProxy);
    Statemachine.fn = Statemachine.prototype;
    Statemachine.fn.add = functino(controller) {
        this.on("change", function(current) {
             if (controller == current) {
                   controller.activate();
             } else {
                   controller.deactivate();
             }
        });
    
        contrlller.active = function() {
             this.trigger("change", controller);
        }.bind(this);
    };

    触发方式很简单

    var  sm = new Statemachine;
    sm.add(ctrl1);
    sm.add(ctrl2);
    sm.add(ctrl3);
    
    ctrl1.active(); // sm.trigger("change", ctrl1);

    把状态和路由建立关系则可以构建单页面应用了.

  • 相关阅读:
    jquery 遮罩层显示img
    redis 模糊查找keys
    consul windows安装
    redis cluster以及master-slave在windows下环境搭建
    c# 设置和取消文件夹共享及执行Dos命令
    svg教程
    mybatis高级查询
    css常用技巧1
    ssm搭建,maven,javaConfig
    MyBatis整体架构
  • 原文地址:https://www.cnblogs.com/zhoulingfeng/p/4308959.html
Copyright © 2011-2022 走看看