Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KsLite-for-3rd-Part-Content-Develop

limu
July 31, 2012

 KsLite-for-3rd-Part-Content-Develop

limu

July 31, 2012
Tweet

More Decks by limu

Other Decks in Technology

Transcript

  1. inf.js-3.0 <!-- http://anydomain/any.js include the content of inf.js --> <script

    src="http://anydomain/any.js"></script> <script> alimama_pid="mm_1_2_3";alimama_type=2; alimama_width=270;alimama_height=390; window.alimama_show && alimama_show(); </script>
  2. http://a.com/a.html: alimama_show() <script src="inf.js"></script> <script> alimama_show(); </script> <iframe src="" style="display:none

    !important" id="anchor-pid"> <!-- ad content--> </iframe> <head> <script src="main.js"></script> </head> <div> <!-- ad content--> </div> destory iframe onunload onbeforeunload
  3. Inf.js – 3.0 需求&功能 •  需求 •  必须稳定 •  足够小

    •  可扩展 -- 结构化 •  功能 •  "alimama_" 变量收集 •  锚点/容器 iframe 输出 •  动态内容无阻引入 •  kissyLite
  4. KsLite -- 基于包的扩展 包内无限可扩展 => 模块名由包名,路径,文件名.三部分构成. {packagename} - [ {path_0}

    - ... - {path_n} - ] {filename} S.Config.lt_pkgs={ inf:"http://a.alimama.cn/kslite/", test:"http://demo.taobao.com/tbad/kslite/" } 模块"inf-a“: http://a.alimama.cn/kslite/inf/a.js 模块"test-t-1“: http://demo.taobao.com/tbad/kslite/test/t/1.js
  5. KsLite -- 基于包的扩展 可扩展无限包 => package root router 在一个地址记录所有可用的包以及对应的class root.

    S.Config.lt_pkgrouter = http://a.alimama.cn/kslite/router.js S.mix(S.Config.lt_pkgs,{ pkg1:"http://a.alimama.cn/pkg1/", pkg2:"http://demo.taobao.com/tbad/pkg2/" }); 当自带S.Config.lt_pkgs没有相关配置时询问pkgrouter.
  6. KsLite -- 命名约定 S.add("pkg1-path1-mod1",function(S,P){ S["pkg1"] = S["pkg1"] || {}; P

    = S["pkg1"]; }); S.use("pkg1-path1-mod1",function(S,P){}); 保证包内对象都在名称空间P,即S.P内. 相当于另一个途径实现S.app(),嵌入至S的app. 为了被Kissy兼容,P只能作为开发约定,手动写在每个包中.
  7. KsLite -- 轻量的add S.add = function(name, fn, config){ var mods

    = S.Env.mods, mod; if (mods[name] && mods[name].status > INIT)return; mod = {name: name,fn: fn || null,status: LOADED}; mods[name] = S.mix(mod,config); } 不提前attach,保证模块在使用之前没有多余的代码执行消耗. 可选优化:domready之前按需执行,domready之后选择性预热.
  8. KsLite -- 简单的use S.use = function(modNames, callback){ var mods =

    S.Env.mods; modNames = modNames.split(','); S.attachMods(modNames, function(){ if (callback) callback(S); }); } 将attachMods单独提出来.不止供use中使用.
  9. asyncer -- 异步执行单元 /* * @interface asyncer 可能需要等待再回调的function. * @param

    { * } args * @param { Function | Object } callback info * @param {number} timeout(ms) * @return {Object} */ function ayncer(args , callback,timeout){ }
  10. parallel asyncers L asyncerA(a,function(resA){ window.resA = resA; if(window.resB)c(); }); asyncerB(b,function(resB){

    window.resB = resB; if(window.resA)c(); }) function c(){ //your code run after resA & resB both returend. }
  11. S.multiAsync J S.multiAsync(asyncers,callback,timeout,isSerial); 实例: S.attachMods = function(modNames,callback){ var i, asyncers

    = {}; for (i = 0; i < modNames.length; i++) { asyncers[modNames[i]] = { f: S.attachMod, a: modNames[i] }; } S.multiAsync(asyncers, callback); }
  12. 串行模块加载优化 场景: mod-a requires mod-b mod-b requires mod-c 开发时: S.use(“mod-a”,function(){});

    //串行的载入mod-a,mod-b,mod-c.效率不高 代码开发完成,发布上线之前,手动预编译优化(就是Google Closure做事的时候): S.log("should use('mod-a,mod-b,mod-c') here for parallel download !"); S.log("should combine 'mod/b.js','mod/c.js' to 'mod/a.js' for reduce requests !");
  13. 避免循环引用实例 Load mod-c => requires a 依赖关系: a => b

    , c , a b => c , a c => a a依赖a throw Error.
  14. End