目的

我们的目的就是编写一个用于创建和管理就地编辑域的可重用的模块化API。它是指网页上的一段普通文本被点击后就变成一个配有一些按钮的表单域,以便用户就地对这段文本进行编辑。

思路

当用户点击时

1.将普通文本域隐藏

2.添加表单元素

3.设置表单元素的value

当用户保存时

ajax通信保存内容

当用户取消时

1.隐藏表单域

2.显示文本域

3.设置文本域的value

类式继承实现就地编辑

superClass的实现(input)

function EditInPlaceField(id, parent, value){
this.id = id;
this.value = value || 'default value';
this.parentElement = parent; this.createElements(this.id);
this.attachEvents();
}
EditInPlaceField.prototype = {
createElements: function(){
this.containerElement = document.createElement('div');
this.parentElement.appendChild(this.containerElement); this.staticElement = document.createElement('span');
this.containerElement.appendChild(this.staticElement);
this.staticElement.innerHTML = this.value; this.fieldElement = document.createElement('input');
this.fieldElement.type = 'text';
this.fieldElement.value = this.value;
this.containerElement.appendChild(this.fieldElement); this.saveButton = document.createElement('input');
this.saveButton.type='button';
this.saveButton.value="Save";
this.containerElement.appendChild(this.saveButton); this.cancelBtton = document.createElement('input');
this.cancelBtton.type = 'button';
this.cancelBtton.value = 'Cancel';
this.containerElement.appendChild(this.cancelBtton);
this.convertToText();
},
attachEvents: function(){
var that = this;
addEvent(this.staticElement, 'click', function(){that.convertToEditable();});
addEvent(this.saveButton, 'click', function(){ that.save();});
addEvent(this.cancelBtton, 'click', function(){that.cancel()});
},
convertToEditable: function(){
this.staticElement.style.display = 'none';
this.fieldElement.style.display = 'inline';
this.saveButton.style.display = 'inline';
this.cancelBtton.style.display = 'inline'; this.setValue(this.value);
},
save: function(){
this.value = this.getValue();
var that = this;
var callback = {
success: function(){that.convertToText();},
failure: function(){alert('Error saving value.');}
};
ajaxRequest('GET', 'save.php?id'+this.id+'&value='+this.value, callback);
},
cancel: function(){
this.convertToText();
},
convertToText: function(){
this.fieldElement.style.display = 'none';
this.saveButton.style.display = 'none';
this.cancelBtton.style.display = 'none';
this.staticElement.style.display = 'inline'; this.setValue(this.value);
},
setValue: function(value){
this.fieldElement.value = value;
this.staticElement.innerHTML = value;
},
getValue: function(){
return this.fieldElement.value;
}
};

subClass的实现(textarea)

function EditInPlaceArea(id, parent, value){
EditInPlaceArea.superclass.constructor.call(this, id, parent, value);
}
extend(EditInPlaceArea, EditInPlaceField);
EditInPlaceArea.prototype.createElements = function(id){
this.containerElement = document.createElement('div');
this.parentElement.appendChild(this.containerElement); this.staticElement = document.createElement('p');
this.containerElement.appendChild(this.staticElement);
this.staticElement.innerHTML = this.value; this.fieldElement = document.createElement('textarea');
this.fieldElement.type = 'text';
this.fieldElement.value = this.value;
this.containerElement.appendChild(this.fieldElement); this.saveButton = document.createElement('input');
this.saveButton.type='button';
this.saveButton.value="Save";
this.containerElement.appendChild(this.saveButton); this.cancelBtton = document.createElement('input');
this.cancelBtton.type = 'button';
this.cancelBtton.value = 'Cancel';
this.containerElement.appendChild(this.cancelBtton);
this.convertToText();
};
EditInPlaceArea.prototype.convertToEditable = function(){
this.staticElement.style.display = 'none';
this.fieldElement.style.display = 'block';
this.saveButton.style.display = 'inline';
this.cancelBtton.style.display = 'inline'; this.setValue(this.value);
};
EditInPlaceArea.prototype.convertToText = function(){
this.fieldElement.style.display = 'none';
this.saveButton.style.display = 'none';
this.cancelBtton.style.display = 'none';
this.staticElement.style.display = 'block'; this.setValue(this.value);
};

API的依赖与调用

addEvent依赖

function addEvent(el, ty, fn){
if(el.addEvetListener){
el.addEvetListener(ty, fn, false);
}else if(el.attachEvent){
el.attachEvent('on'+ty, fn);
}else{
el['on'+ty] = fn;
}
}

extend依赖

function extend(subClass, superClass){
var F = function(){};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor){
superClass.prototype.constructor = superClass;
}
}

API的调用

var titleClassical = new EditInPlaceField('titleClassical', document.getElementById('titleClassical').parentNode, 'Title here');
var bodyClassical =new EditInPlaceArea('bodyClassical', document.getElementById('bodyClassical').parentNode, 'Body here');

效果展示

最开始时

点击后

保存后

取消后

原型式继承和掺元类

基本代码就那些,原型式继承和掺元类的实现只是模式的不同,所以就不再给出具体代码了。

JS设计模式——4.继承(示例)的更多相关文章

  1. JS设计模式——4.继承(概念)

    类式继承 0.构造函数 一个简单的Person类 function Person(name){ this.name = name; } Person.prototype.getName = funct ...

  2. JS设计模式(一)

    刚入职时,看过一段时间的设计模式,似懂非懂.不知不觉过去七个月了,对JS的理解更深刻了,数据结构与算法的基础也基本上算是过了一遍了,接下来要把设计模式搞定,然后不再深层次研究JS了,而是学习前端自动化 ...

  3. JS如何实现继承?

    JS的继承是基于JS类的基础上的一种代码复用机制.换言之,有了代码,我们就不需要复制之前写好的方法,只要通过简捷的方式 复用之前自己写的或同事写的代码.比如一个弹出层,我们需要在上面做一些修改.同事写 ...

  4. js设计模式——5.状态模式

    js设计模式——5.状态模式 代码演示 /*js设计模式——状态模式*/ // 状态(红灯,黄灯,绿灯) class State { constructor(color) { this.color = ...

  5. js设计模式——1.代理模式

    js设计模式——1.代理模式 以下是代码示例 /*js设计模式——代理模式*/ class ReadImg { constructor(fileName) { this.fileName = file ...

  6. 浅谈JS中的继承

    前言 JS 是没有继承的,不过可以曲线救国,利用构造函数.原型等方法实现继承的功能. var o=new Object(); 其实用构造函数实例化一个对象,就是继承,这里可以使用Object中的所有属 ...

  7. JS创建对象、继承原型、ES6中class继承

    面向对象编程:java中对象的两个基本概念:1.类:类是对象的模板,比如说Leader 这个是泛称领导,并不特指谁.2:实例:实例是根据类创建的对象,根据类Leader可以创建出很多实例:liyi,y ...

  8. js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

    js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } Class ...

  9. js设计模式(12)---职责链模式

    0.前言 老实讲,看设计模式真得很痛苦,一则阅读过的代码太少:二则从来或者从没意识到使用过这些东西.所以我采用了看书(<js设计模式>)和阅读博客(大叔.alloyteam.聂微东)相结合 ...

随机推荐

  1. PHP 生成条形码

    <?php class BarCode128 { const STARTA = 103; const STARTB = 104; const STARTC = 105; const STOP = ...

  2. Halcon 笔记1

    Halcon Example位置: C:\Users\Public\Documents\MVTec\HALCON-13.0\examples 安装位置:C:\Program Files\MVTec\H ...

  3. Java进行Base64的编码(Encode)与解码(Decode)

    关于base64编码Encode和Decode编码的几种方式 Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便 ...

  4. vue ui components

    vue ui components h_ui https://www.npmjs.com/~hs_ui https://www.npmjs.com/package/h_ui_beta https:// ...

  5. HDU4675_GCD of Sequence

    很有意思的一个数论题. 是这样的,给你一个数数组a[i],其中给出的每一个数字和要求的数字方位都是[1,m],现在问你根据a[]构造一个b[],且a和b中间的不相等的元素的个数恰好有k个. 现在问你g ...

  6. Struts的default.properties五个配置 一般利用按着配置文件的加载的顺序,后面文件和前面文件相同的配置,后面的会把前面的文件的值覆盖的原则 在struts.xml里面进行配置

    1 struts.i18n.encoding=UTF-8 配置编码 2 struts.action.extension=action,, 配置浏览器访问地址的后缀 3 struts.devMode = ...

  7. java 调试

    作为一名java开发程序员,或者有时候需要利用工具调试的时候,但是却感觉不会使用,其实只要记住四个键即可. 一般java开发工具使用的都是Eclipse或者MyEclipse,下面都有这几个键F5(进 ...

  8. Android APK 反编译步骤

    dex2jar和jd-gui工具下载,链接:http://yun.baidu.com/share/link?shareid=2888715259&uk=1377615098 解压APK文件得到 ...

  9. Static全局变量(函数)与普通的全局变量(函数)的区别

    转自:http://www.cnblogs.com/zjvskn/p/5548879.html Static全局变量与普通的全局变量有什么区别? 答: 全局变量(外部变量)的说明之前再冠以static ...

  10. HDU 4383 To The Moon 解题报告

    HDU 4383 To The Moon 题意翻译 已知一个长为\(n\)的序列\(a\),你需要进行下面的四种操作. C l r d 将区间\([l,r]\)中的数加上\(d\),同时时间加\(1\ ...