zoukankan      html  css  js  c++  java
  • 使用局部上下文创建控制器对象

    创建绑定this上下文环境的并且可重用的控制器对象,

    //this使用局部上下文基础模型
    (function($, exports){
     var mod = function(includes){
      if(includes) this.include(includes);
     };
     mod.fn = mod.prototype;
    
     //将函数上下文this绑定到mod上
     mod.fn.proxy = function(func){
      return $.proxy(func, this);
     };
    
     //documentContent加载完后执行相应的func回调
     mod.fn.load = function(func){
      $(this.proxy(func));
     };
    
     //扩展prototype的方法
     mod.fn.include = function(ob){
      $.extend(this, ob);
     };
    
     //导出为Controller全局对象
     exports.Controller = mod;
    })(jQuery, window);
    
    //给局部上下文模型扩展方法
    Controller.fn.unload = function(func){
     jQuery(window).bind('unload', this.proxy(func));
    };
    
    
    //创建Controller对象模块
    (function($, Controller){
    var mod = new Controller;
    
    mod.toggleClass = function(e){
     console.log(this);
     this.view.toggleClass('over', e.data);
    };
    
    //文档加载完毕,可视作window.onload
    mod.load(function(){
     this.view = $('#view');
    
     //绑定mod实例到this.goggleClass,以防止toggleClass内的this指向mouseover事件函数
     this.view.bind('mouseover', true, this.proxy(this.toggleClass));
     this.view.bind('mouseout', false, this.proxy(this.toggleClass));
    });
    var abc = function(aa){
     console.log(1111);
    };
    mod.include({abc:abc});
    
    
    //test 创建第二个控制器对象
    var mod1 = new Controller();
    console.log(mod1);
    
    
    })(jQuery, Controller);
    ================================我是分割 线==========================================
     
    第一步:创建控制器模型
    这里最主要的是使用了jquery的$.proxy函数来实现上下文的绑定,并且使用exports.Controller方式来爆露导出mod方式,减少了全局对象的污染
    第二步:生成控制器模型对象
    每一个控制器内都可创建了控制器模型的实例,这样就通过prototype继承方式继承了load等所有函数
  • 相关阅读:
    【Python大系】Python快速教程
    【Linux大系】Linux的概念与体系
    【Java大系】Java快速教程
    【夯实PHP系列】PHP正则表达式
    【PHP夯实基础系列】PHP日期,文件系统等知识点
    【夯实PHP系列】购物车代码说明PHP的匿名函数
    2016.09.21 公司裁员想到的
    使用android-junit-report.jar导出单元测试报告
    APK无源码使用Robotium简单总结
    Android环境安装简单总结
  • 原文地址:https://www.cnblogs.com/willian/p/2880467.html
Copyright © 2011-2022 走看看