javascript-模板方法模式-提示框归一化插件
模板方法模式笔记
父类中定义一组算法操作骨架,而将一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤
实例:弹出框归一化插件
css样式
.alert{position: fixed;left: 50%;top: 50%;background-color: #ffffff;z-index:;width:400px;
border-radius: 5px;font-weight: bold;color: #535e66;box-shadow: 1px 1px 4px #eee,-1px -1px 4px #eee;margin-top:-9rem;margin-left:-200px;}
.alert h3{text-indent:2rem;border-bottom:1px solid #eef0f1;margin:;padding:1rem 0;}
.alert p{padding: 2rem 5rem;height: 5rem;overflow: hidden;margin:;border-bottom:1px solid #eef0f1;}
.alert .a-close{display: block;cursor: pointer;width: 12px;height: 12px;position: absolute;top: 1.2rem;right: 1.5rem;background: url(../img/icons.png) -48px -96px no-repeat;}
.alert .a-close:after{display: block;content: ""; clear: both;}
.alert .btn{display: inline-block;cursor: pointer;width: 4.5rem;height: 1.8rem;line-height: 1.8rem;text-align: center;color: #FFFFFF;
border-radius: 5px;margin:0.5rem;}
.alert .panelFooter{width:11rem;float:right;}
.alert .a-confirm{background-color: #0095d9;}
.alert .cancel{background-color: #546a79;}
.alert .right{float:right}
@media only screen and (min-width: 100px) and (max-width: 640px) {
.alert{width:80%;margin-left:-40%;}
.alert h3{text-indent: 1rem;}
.alert p{padding:1rem;}
.alert .a-close{right: 1rem;}
}
运用寄生组合继承方法
//原型式继承
function inheritobject(o){
//声明一个过渡函数对象
function F(){
}
//过渡原型对象继承父对象
F.prototype=o;
//返回过渡对象的一个实列,该实例的原型继承了父对象
return new F();
} /*
*寄生式继承 继承原型
* 传递参数subclass 子类
* 传递参数superclass 父类
* */
function inheritPrototype(subclass,superclass){
//复制一份父类的原型副本保存在变量中
var p=inheritobject(superclass.prototype);
//修正因为重写子类原型导致子类的constructor属性被修改
p.constructor=subclass;
//设置子类原型
subclass.prototype=p;
}
首先要创建基本提示框基类(模板类),然后其他提示框类只需要在继承的基础上,拓展自己所需即可,日后需求变更,修改基础类,所有提示框类实现的样式都会统一变化
基本提示框类:只实现一个提示内容、一个关闭按钮、一个确定按钮
var Alert = function(data){
if(!data) return;
this.content=data.content;
this.panel =document.createElement("div");//提示框面板
this.contentNode=document.createElement("p");
this.panelFooter=document.createElement("div");
this.confirmBtn=document.createElement("span");
this.closeBtn=document.createElement("b");
this.panel.className="alert";
this.panelFooter.className="panelFooter";
this.confirmBtn.className="btn a-confirm";
this.closeBtn.className="a-close";
this.confirmBtn.innerHTML=data.confirm || '确认';
this.contentNode.innerHTML=this.content;
this.success=data.success || function(){};
this.fail=data.fail || function(){};
}
Alert.prototype={
init :function(){
this.panel.appendChild(this.closeBtn);
this.panel.appendChild(this.contentNode);
this.panelFooter.appendChild(this.confirmBtn);
this.panel.appendChild(this.panelFooter);
document.body.appendChild(this.panel);
this.bindEvent();
this.show();
},
bindEvent : function(){
var me = this;
this.closeBtn.onclick = function(){
me.fail();
me.hide();
}
this.confirmBtn.onclick = function(){
me.success();
me.hide();
}
},
hide : function(){
this.panel.style.display ="none";
},
show :function(){
this.panel.style.display='block';
}
}
右侧按钮提示框类:继承基本提示框类,拓展需求,使按钮移动到右侧显示
//右侧按钮提示框
var RightAlert =function(data){
Alert.call(this,data);
this.panelFooter.className = this.panelFooter.className + ' right';
}
inheritPrototype(RightAlert,Alert);
标题提示框类 :继承基本提示框类,拓展需求,添加一个提示框标题
//标题提示框类
var TitleAlert=function(data){
Alert.call(this,data);
this.title = data.title;
this.titleNode = document.createElement("h3");
this.titleNode.innerHTML = this.title;
}
inheritPrototype(TitleAlert,Alert);
TitleAlert.prototype.init =function(){
this.panel.insertBefore(this.titleNode,this.panel.firstChild);
Alert.prototype.init.call(this); }
继承类也可作为模板
带有取消按钮的弹出框:继承标题提示框类,拓展需求,添加一个取消按钮
//继承类也可作为模板
var CancelAlert = function(data){
//继承标题提示框构造函数
TitleAlert.call(this,data);
//取消按钮文案
this.cancel = data.cancel;
this.cancelBtn = document.createElement('span');
this.cancelBtn.className='btn cancel';
this.cancelBtn.innerHTML = this.cancel || '取消';
}
inheritPrototype(CancelAlert,TitleAlert);
CancelAlert.prototype.init = function(){
TitleAlert.prototype.init.call(this);
this.panelFooter.appendChild(this.cancelBtn);
}
CancelAlert.prototype.bindEvent = function(){
var me=this;
TitleAlert.prototype.bindEvent.call(me);
this.cancelBtn.onclick = function(){
me.fail();
me.hide();
}
}
测试代码
new CancelAlert({
title : '提示',
content : '提示内容',
success : function(){
console.log('ok');
},
fail : function(){
console.log("cancel");
}
}).init();
浏览器显示

javascript-模板方法模式-提示框归一化插件的更多相关文章
- javascript模板方法模式
一:什么是模板方法模式: 模板方法模式由二部分组成,第一部分是抽象父类,第二部分是具体实现的子类,一般的情况下是抽象父类封装了子类的算法框架,包括实现一些公共方法及封装子类中所有方法的执行顺序,子类可 ...
- 轻松掌握:JavaScript模板方法模式
模板方法模式 假如我们有一些对象,各个对象之间有一些相同的行为,也有一些不同的行为,这时,我们就可以用模板方法模式来把相同的部分上移到它们的共同原型中(父类),而将不同的部分留给自己各自重新实现. 模 ...
- Jquery 弹出提示框输入插件 apprise 修改中文按钮以及使用说明
apprise的使用非常简单,引入js脚本和css <script type="text/javascript" src="/js/apprise-1.5.fu ...
- jQuery警告/确认/提示弹出对话框效果(替换传统JavaScript下的提示框)
http://www.51xuediannao.com/js/jquery/jquery_tsk/ http://www.jq22.com/demo/jqueryConfirm20160413/
- javascript中简单提示框
CSS部分 .help-tip{ width: 340px; border:1px solid #A0A0A0; background-color: #F8F8F8; border-radius: 5 ...
- JavaScript实现延时提示框
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- 基于jquery的提示框JavaScript 插件,类Bootstrap
目录 基于jquery的提示框JavaScript 插件,类Bootstrap 基于jquery的提示框JavaScript 插件,类Bootstrap 源码 github地址: https://gi ...
- 再起航,我的学习笔记之JavaScript设计模式17(模板方法模式)
模板方法模式 由模板方法模式开始我们正式告别结构型设计模式,开始行为型设计模式的学习分享 行为型设计模式用于不同对象之间职责划分或算法抽象,行为型设计模式不仅仅涉及类和对象,还涉及类或对象之间的交流模 ...
- javascript设计模式(张容铭)学习笔记 - 照猫画虎-模板方法模式
模板方法模式(Template Method):父类中定义一组操作算法骨架,而降一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤. 项目经理体验了各个页面的 ...
随机推荐
- Electron 不完全快速手册
Electron能干嘛 Vscode 基于Electron开发的,他是用来开发桌面软件的,可以轻易的跨平台 他的前身是atomshell,图标很丑,不用在意,一点也不像vscode也不用在意. L ...
- 苹果手机Safari无痕浏览模式下系统登录成功但是页面不跳转
昨天下午,测试提了一个bug,问题是:在苹果手机Safari无痕浏览模式下系统登录成功但是页面不跳转. 思前想后找了半天没思路,后来经过同事的点拨,说可能是禁用了cookie之类的,反正我也没思路就顺 ...
- Java关键字:static
通常,当创建类时,就是在描述那个类的外观和行为.只有用new创建类的对象时,才分配数据存储空间,方法才能被调用.但往往我们会有下面两种需求: 1.我想要这样一个存储空间:不管创建多少对象,无论是不创建 ...
- MongoDB 存储引擎:WiredTiger和In-Memory
存储引擎(Storage Engine)是MongoDB的核心组件,负责管理数据如何存储在硬盘(Disk)和内存(Memory)上.从MongoDB 3.2 版本开始,MongoDB 支持多数据存储引 ...
- JavaScript权威设计--JavaScript变量,作用域,声明提前(简要学习笔记四)
1.宿主对象与宿主环境 宿主对象:由ECMAScript实现的宿主环境提供的对象,可以理解为:浏览器提供的对象.所有的BOM和DOM都是宿主对象. 宿主环境:一般宿主环境由外壳程序创建与维护,只要 ...
- [OAuth]基于DotNetOpenAuth实现Client Credentials Grant
Client Credentials Grant是指直接由Client向Authorization Server请求access token,无需用户(Resource Owner)的授权.比如我们提 ...
- Restful 介绍及SpringMVC+restful 实例讲解
restful不是一个框架,称为一种编码更烦更贴切吧,其核心类位于spring-web.jar中,即RestTemplate.class restful是rpc通过http协议的一种实现方式,和web ...
- Core Java 总结(关键字,特性问题)
2016-10-19 说说&和&&的区别 初级问题,但是还是加入了笔记,因为得满分不容易. &和&&都可以用作逻辑与的运算(两边是boolean类型), ...
- 本博客现已迁移到chuxiuhong.com
欢迎大家访问,我会暂时保留这个博客的更新,实现两个博客的同步. 新博客地址: http://chuxiuhong.com
- Kafka资源汇总
终于下定决心写一点普及类的东西.很多同学对Kafka的使用很感兴趣.如果你想参与到Kafka的项目开发中,很多资源是你必须要提前准备好的.本文罗列了一些常用的Kafka资源,希望对这些develope ...