kokiya的核心js代码,今天弄的。基本思路是分离dom和js代码,一个模块一个模块加载。争取尽量多的代码能够重用。
/* Author:
Spencer Zhang
sepmein@gmail.com
Copy right reserved
*/
// Add an Object method that doesn't // affect for/in loops
Object.defineProperty(
Object.prototype,
"extend",
{
enumerable: false,
writable: true,
configurable: true,
value: function (from) {
var names = Object.getOwnPropertyNames(from);
names.forEach(function (n) {
if (n in this) return;
var d = Object.getOwnPropertyDescriptor(from, n);
Object.defineProperty(this, n, d);
});
}
}
);
/*core*/
var kokiya = Object.create(Object.prototype);
Object.defineProperties(kokiya, {
"testResult" : {
/*
测试浏览器的兼容性。返回一个对象。包含一个
目前只测试浏览器是否使用html5 localStorage储存过小毛的文。
使用testResult动态加载js文件。
将来扩展。
*/
value : (function () {
var result = {};
/*测测以前有米有上过*/
result.storaged = Modernizr.localstorage && Boolean((localStorage.getItem('直升班——结构')) && Boolean(localStorage.getItem('1')) && Boolean(localStorage.getItem('印象——过渡')));
return result;
})(),
writable : true,
configurable : true,
enumerable : true
},
/*init kokiya*/
"init" : {
/*
在loadModule函数作用完后,递归执行被加载入kokiya.module数组的模块。
成功后在console记录一条消息。
目前没有参数。
*/
value : function () {
for (var i = this.module.length - 1; i >= 0; i--) {
for (var prop in this.module[i]){
this.module[i][prop]();
/*for debug*/
console.log("Module "+prop+" loaded");
}
};
},
writable : true,
configurable : true,
enumerable : true
},
"module" : {
/*
一个数组,由Spencer臆想出来的模块的组合
*/
value : [],
writable : true,
configurable : true,
enumerable : true
},
"loadModule" : {
/*
加载模块,加载的参数称为“模块”。(模块是由spencer臆想出来的东西)
“模块”可以是一个由对象构成的数组,也可以是一个对象。
无论是数组还是对象,这个对象都由{”模块名“:”函数体“}构成。
所加载的模块自动推送到kokiya.module数组。
*/
value : function (m) {
if( Array.isArray(m) ) {
for (var i = m.length - 1; i >= 0; i--) {
this.module.push(m[i]);
};
} else {
this.module.push(m);
}
},
writable : true,
configurable : true,
enumerable : true
}
});
kokiya.loadModule(
/*为link增加bootstrap popover效果*/
{
"popover" : function () { $('footer a').popover(); }
}
);
kokiya.init();
这边留个底先,不成熟的想法容易忘。