1.配置文件config.js
Config = function( config ) {
//组件,key是组件ID,value是组件对象
config.plugins =[{'type1':'Type1'},{'type2':'Type2'}];
return config;
};
2.TypeAll对象
TypeAll.create = function(t) {
if(t instanceof TypeAll) {
return t.test();
}
}
3.组件对象
function Type1() {
this.test = function() {
//具体实现
}
}
Type1.prototype = new TypeAll();//继承TypeAll
function Type2() {
this.test = function() {
//具体实现
}
}
Type2.prototype = new TypeAll();//继承TypeAll
4.具体调用
var config = {};
config = new Config(config);
var plugins = config.plugins;//获取配置关系
$.each(plugins,function(i,n){
for (var key in n){
var pObj=eval(n[key]);
if (id==key){//id是外部传入组件id
var t= new pObj();
html = TypeAll.create(t);
break;
}
}
});
这是我在做一个页面设计器代码重构过程中,处理的,可能还有很多不合理的地方,学习不够深入,希望大家指正。

本文介绍了一个页面设计器的代码重构过程,重点在于组件配置与实例化的处理方式,通过使用Config类来管理不同类型的组件,并实现了Type1和Type2两种组件的具体功能。
1495

被折叠的 条评论
为什么被折叠?



