Permalink
Browse files

add adapter.js, improve readme

  • Loading branch information...
dexteryy committed Oct 13, 2012
1 parent 6db2dae commit 2643434ad01e46fb052dc2d11d06fdf07bcd0170
Showing with 567 additions and 1 deletion.
  1. +13 −1 README.md
  2. +111 −0 adapter.js
  3. +362 −0 examples/adapter/css/main.css
  4. +81 −0 examples/adapter/index.html
View
@@ -8,6 +8,12 @@ oz.js是一个比RequireJS历史更悠久的AMD/浏览器端模块实现,表
`/examples`相当于文档。
+## Getting Started
+
+我知道解答“我想试试,怎样开始?”的文档是最重要的,但暂时还没写嘛……先参考这些例子罢:
+
+* Usages with oz.js & ozma.js: [demo1(开发环境或生产环境)](http://dexteryy.github.com/OzJS/examples/buildtool/demo1.html) [demo2(开发环境)](http://dexteryy.github.com/OzJS/examples/buildtool/demo2.html) [demo3(生产环境)](http://dexteryy.github.com/OzJS/examples/buildtool/demo3.html) [demo4(使用第三方包管理系统)](http://dexteryy.github.com/OzJS/examples/buildtool/demo4.html)
+* WebApp demo: [Doubanchou](https://github.com/dexteryy/doubanchou)
## Tutorials
@@ -18,13 +24,17 @@ oz.js是一个比RequireJS历史更悠久的AMD/浏览器端模块实现,表
例子都写的糙猛快,信息量都在源码里
-* OzJS Builder (Ozma.js): [usage](http://dexteryy.github.com/OzJS/examples/buildtool/index.html) [demo1](http://dexteryy.github.com/OzJS/examples/buildtool/demo1.html) [demo2](http://dexteryy.github.com/OzJS/examples/buildtool/demo2.html) [demo3](http://dexteryy.github.com/OzJS/examples/buildtool/demo3.html) [demo4](http://dexteryy.github.com/OzJS/examples/buildtool/demo4.html)
+* OzJS Builder (Ozma.js): [usage](http://dexteryy.github.com/OzJS/examples/buildtool/index.html)
+* OzJS Adapter: [demo](http://dexteryy.github.com/OzJS/examples/adapter/index.html)
* mod/lang: [demo](http://dexteryy.github.com/OzJS/examples/lang/index.html)
* mod/event: [demo](http://dexteryy.github.com/OzJS/examples/event/index.html)
+* mod/dollar: [demo](http://dexteryy.github.com/OzJS/examples/dollar/index.html)
* mod/animate: [demo](http://dexteryy.github.com/OzJS/examples/animate/index.html)
## In the Real World
+OzJS的设计和开发都偏好自底向上的原则,无论灵感、观念还是代码都完全来自真实的、面向终端用户的互联网产品
+
* [Alphatown](http://alphatown.com) *2D browser based virtual world*
* [Douban Reader](http://read.douban.com/reader) *Web browser based e-book reader*
* [Douban's contributor system](http://read.douban.com/submit/) *Online self-publishing tool for Douban Reader*
@@ -39,6 +49,8 @@ oz.js是一个比RequireJS历史更悠久的AMD/浏览器端模块实现,表
## Changelog
+* `mod/event` 增加promise.pipe接口,某些API加了别名 [示例和文档](http://dexteryy.github.com/OzJS/examples/event/index.html)
+* `mod/dollar` 发布beta版 [示例和文档](http://dexteryy.github.com/OzJS/examples/dollar/index.html)
* `mod/lang` 增加示例
* `Ozma.js` 1.1.0 发布,`npm`可更新
* `Ozma.js`对动态加载模块的多路径依赖问题提供完善的构建支持,[用例演示](http://github.com/dexteryy/OzJS/blob/master/examples/buildtool/js/app.js)
View
@@ -0,0 +1,111 @@
+/**
+ * mini define/require mplementation for old web page
+ * transformed AMD module into traditional module pattern
+ * demo:
+ * see http://dexteryy.github.com/OzJS/ for details
+ *
+ * Copyright (C) 2010-2012, Dexter.Yy, MIT License
+ * vim: et:ts=4:sw=4:sts=4
+ */
+
+// 这个define的实现会让amd模块声明变成传统的module pattern
+function define(fullname, deps, block){
+ if (!block) {
+ if (deps) {
+ block = deps;
+ } else {
+ block = fullname;
+ fullname = [];
+ }
+ if (typeof fullname !== 'string') {
+ deps = fullname;
+ fullname = "";
+ } else {
+ deps = [];
+ }
+ }
+ var callee = define,
+ len = deps.length,
+ exports = {},
+ opt = callee._global_exports[fullname] || {},
+ current_ns = callee._current_ns = opt.ns
+ || callee._current_ns || callee._ns || 'window';
+ deps = ((/^function.*?\(([\w'"\/\-\:,\n\r\s]*)\)/
+ .exec(block.toString()) || [])[1] || '')
+ .replace(/\s+/g, '').split(',');
+ deps.length = len;
+ var args = deps.map(function(name){
+ return (window[current_ns] || {})[name];
+ });
+ args.push(function(reqs, callback){
+ if (callback) {
+ require(reqs, callback);
+ } else {
+ return (callee._global_exports[reqs] || {}).exports;
+ }
+ });
+ args.push(exports);
+ exports = opt.exports = block.apply(this, args) || exports;
+ if (opt.names) {
+ opt.names.forEach(function(name){
+ name = name.split('.');
+ var context = window, i = name[0];
+ if (name[1]) {
+ context = context[i] = context[i] || {};
+ i = name[1];
+ }
+ context[i] = exports;
+ });
+ }
+ callee._current_ns = null;
+}
+
+// require的实现跟define一样,不过是异步的
+function require(reqs){
+ if (typeof reqs === 'string') {
+ reqs = [reqs];
+ }
+ var args = arguments;
+ setTimeout(function(){
+ define.apply(this, args);
+ }, 0);
+}
+
+define._global_exports = {};
+
+// 为模块设置命名空间,既可以避免产生全局变量,
+// 也可以隔离不同体系的模块(比如'mod/event'和'jquery/event'),避免命名冲突
+define.ns = function(mid, namespace){
+ if (!namespace) {
+ this._ns = mid;
+ } else {
+ var opt = this._global_exports;
+ if (!opt[mid]) {
+ opt[mid] = {
+ names: []
+ };
+ }
+ opt[mid].ns = namespace;
+ }
+};
+
+// 将模块的exports映射到全局命名空间下,让没有封装到amd模块里的代码也可以使用
+define.config = function(mid, vars){
+ if (typeof mid === 'object') {
+ for (var i in mid) {
+ this.config(i, mid[i]);
+ }
+ return;
+ }
+ var opt = this._global_exports;
+ if (!opt[mid]) {
+ opt[mid] = {
+ ns: this._ns,
+ names: []
+ };
+ }
+ if (typeof vars === 'string') {
+ vars = [vars];
+ }
+ [].push.apply(opt[mid].names, vars);
+};
Oops, something went wrong.

0 comments on commit 2643434

Please sign in to comment.