日期:2014-05-16 浏览次数:20538 次

核心实现:
constructor/js/aispeech.js
var AIS = (function() {
var ONE_PROP = 'test'; // 私有属性和私有方法,只供框架内部使用
return {
create: function(obj, supr) { // 创建类或子类
var sb = obj.initialize, sp;
delete obj.initialize;
if (!supr || typeof supr === 'object') {
sb.prototype = obj;
} else {
sp = function() {};
sp.prototype = supr.prototype;
sb.prototype = new sp();
sb.supr = sp.prototype;
$.extend(sb.prototype, obj);
}
sb.prototype.constructor = sb;
return sb;
},
getScript: function(url, cb) { // 引入js文件
$.getScript(url, cb);
},
modual: {}, // 注册模块及其路径
namespace: function(ns, cb) { // 命名空间
var nsArr = ns.split('.'), o;
o = window[nsArr[0]] = window[nsArr[0]] || {};
$.each(nsArr.slice(1), function(i, n) {
o = o[n] = o[n] || {};
});
AIS.modual[nsArr.slice(-1)].ns = ns;
if (cb && typeof cb === 'function') cb.call(o);
},
importMD: function(modual, cb) { // 导入模块,根据模块名
cb = cb || function() {};
if (AIS.modual[modual].ns) {
cb.call(eval(AIS.modual[modual].ns));
} else {
AIS.getScript(AIS.modual[modual].url, function() {
cb.call(eval(AIS.modual[modual].ns));
});
}
},
util: { // 工具集
isEmptyObject: function(obj) {
for (var prop in obj) {
return false;
}
return true;
},
toArray: function(arrLike) {
return Array.prototype.slice(arrLike);
},
// browsers..
isIE: function() {
return !-[1,];
}
}
}
})();使用方法:
var A = AIS.create({
initialize: function(name) { // 每个类都必须声明的构造器方法
this.name = name;
},
hello: function() {
alert('hello: '+ this.name);
}
});
var a = new A('danny');
a.hello();
var B = AIS.create({ // B继承自A
initialize: function(name, age) {
B.supr.constructor.call(this, name);
this.age = age;
},
world: function() {
alert('world: '+ this.age);
}
}, A);constructor/ais.modual.js
AIS.modual = {
'login': {
'url': 'js/login/login.js'
},
'switcher': {
'url': 'js/login/switch.js'
}
}constructor/js/login/login.js
AIS.namespace('com.ais.aid201105.login', function() {
//var PrivateClass = AIS.create({...}); 可以创建包内私有的类
this.LoginForm = AIS.create({ // public class
initialize: function(pid) {
this.parentId = pid;
this.initForm();
this.submit();
},
initForm: function() {
var $f = $('<form></form>');
$f.append('<fieldset style="width: 250px;"></fieldset>').find('fieldset')
.append('<legend>Login Form</legend>')
.append('<label class="username">name: </label> <input type="text" id="username" /><br />')
.append('<label class="password">password: </label> <input type="password" id="password" /><br />')
.append('<label class="gender">gender: </label> <select id="gender"><option>male</option><option>female</option></select><br />')
.append('<input type="button" id="submit" value="submit" /> <input type="reset" va