|
4 | 4 |
|
5 | 5 | 最近萌发了一个想法写游戏引擎,之前想着做一个JavaScript前端框架。看看,这个思路是怎么来的。
|
6 | 6 |
|
7 |
| -##一、[Lettuce](https://github.com/phodal/lettuce)构建过程 |
| 7 | +##[Lettuce](https://github.com/phodal/lettuce)构建过程 |
8 | 8 |
|
9 | 9 | > Lettuce是一个简约的移动开发框架。
|
10 | 10 |
|
|
65 | 65 |
|
66 | 66 | 但是显然,他们都太重了。事实上,对于一个库来说,80%的人只需要其中20%的代码。于是,找到了[https://github.com/stackp/promisejs](https://github.com/stackp/promisejs),看了看用法,这就是我们需要的功能:
|
67 | 67 |
|
68 |
| - function late(n) { |
69 |
| - var p = new promise.Promise(); |
70 |
| - setTimeout(function() { |
71 |
| - p.done(null, n); |
72 |
| - }, n); |
73 |
| - return p; |
| 68 | +```javascript |
| 69 | +function late(n) { |
| 70 | + var p = new promise.Promise(); |
| 71 | + setTimeout(function() { |
| 72 | + p.done(null, n); |
| 73 | + }, n); |
| 74 | + return p; |
| 75 | +} |
| 76 | + |
| 77 | +late(100).then( |
| 78 | + function(err, n) { |
| 79 | + return late(n + 200); |
74 | 80 | }
|
75 |
| - |
76 |
| - late(100).then( |
77 |
| - function(err, n) { |
78 |
| - return late(n + 200); |
79 |
| - } |
80 |
| - ).then( |
81 |
| - function(err, n) { |
82 |
| - return late(n + 300); |
83 |
| - } |
84 |
| - ).then( |
85 |
| - function(err, n) { |
86 |
| - return late(n + 400); |
87 |
| - } |
88 |
| - ).then( |
89 |
| - function(err, n) { |
90 |
| - alert(n); |
91 |
| - } |
92 |
| - ); |
| 81 | +).then( |
| 82 | + function(err, n) { |
| 83 | + return late(n + 300); |
| 84 | + } |
| 85 | +).then( |
| 86 | + function(err, n) { |
| 87 | + return late(n + 400); |
| 88 | + } |
| 89 | +).then( |
| 90 | + function(err, n) { |
| 91 | + alert(n); |
| 92 | + } |
| 93 | +); |
| 94 | +``` |
93 | 95 |
|
94 | 96 | 接着打开看看Promise对象,有我们需要的功能,但是又有一些功能超出我的需求。接着把自己不需要的需求去掉,这里函数最后就变成了
|
95 | 97 |
|
96 |
| - function Promise() { |
97 |
| - this._callbacks = []; |
| 98 | +```javascript |
| 99 | +function Promise() { |
| 100 | + this._callbacks = []; |
| 101 | +} |
| 102 | + |
| 103 | +Promise.prototype.then = function(func, context) { |
| 104 | + var p; |
| 105 | + if (this._isdone) { |
| 106 | + p = func.apply(context, this.result); |
| 107 | + } else { |
| 108 | + p = new Promise(); |
| 109 | + this._callbacks.push(function () { |
| 110 | + var res = func.apply(context, arguments); |
| 111 | + if (res && typeof res.then === 'function') { |
| 112 | + res.then(p.done, p); |
| 113 | + } |
| 114 | + }); |
98 | 115 | }
|
| 116 | + return p; |
| 117 | +}; |
| 118 | + |
| 119 | +Promise.prototype.done = function() { |
| 120 | + this.result = arguments; |
| 121 | + this._isdone = true; |
| 122 | + for (var i = 0; i < this._callbacks.length; i++) { |
| 123 | + this._callbacks[i].apply(null, arguments); |
| 124 | + } |
| 125 | + this._callbacks = []; |
| 126 | +}; |
99 | 127 |
|
100 |
| - Promise.prototype.then = function(func, context) { |
101 |
| - var p; |
102 |
| - if (this._isdone) { |
103 |
| - p = func.apply(context, this.result); |
104 |
| - } else { |
105 |
| - p = new Promise(); |
106 |
| - this._callbacks.push(function () { |
107 |
| - var res = func.apply(context, arguments); |
108 |
| - if (res && typeof res.then === 'function') { |
109 |
| - res.then(p.done, p); |
110 |
| - } |
111 |
| - }); |
112 |
| - } |
113 |
| - return p; |
114 |
| - }; |
115 |
| - |
116 |
| - Promise.prototype.done = function() { |
117 |
| - this.result = arguments; |
118 |
| - this._isdone = true; |
119 |
| - for (var i = 0; i < this._callbacks.length; i++) { |
120 |
| - this._callbacks[i].apply(null, arguments); |
121 |
| - } |
122 |
| - this._callbacks = []; |
123 |
| - }; |
124 |
| - |
125 |
| - var promise = { |
126 |
| - Promise: Promise |
127 |
| - }; |
| 128 | +var promise = { |
| 129 | + Promise: Promise |
| 130 | +}; |
| 131 | +``` |
128 | 132 |
|
129 | 133 | 需要注意的是: ``License``,不同的软件有不同的License,如MIT、GPL等等。最好能在遵循协议的情况下,使用别人的代码。
|
130 | 134 |
|
131 | 135 | ###实现第二个需求
|
132 | 136 |
|
133 | 137 | 由于,现有的一些Ajax库都比较,最后只好参照着别人的代码自己实现。
|
134 | 138 |
|
135 |
| - Lettuce.get = function (url, callback) { |
136 |
| - Lettuce.send(url, 'GET', callback); |
137 |
| - }; |
138 |
| - |
139 |
| - Lettuce.load = function (url, callback) { |
140 |
| - Lettuce.send(url, 'GET', callback); |
141 |
| - }; |
142 |
| - |
143 |
| - Lettuce.post = function (url, data, callback) { |
144 |
| - Lettuce.send(url, 'POST', callback, data); |
145 |
| - }; |
146 |
| - |
147 |
| - Lettuce.send = function (url, method, callback, data) { |
148 |
| - data = data || null; |
149 |
| - var request = new XMLHttpRequest(); |
150 |
| - if (callback instanceof Function) { |
151 |
| - request.onreadystatechange = function () { |
152 |
| - if (request.readyState === 4 && (request.status === 200 || request.status === 0)) { |
153 |
| - callback(request.responseText); |
154 |
| - } |
155 |
| - }; |
156 |
| - } |
157 |
| - request.open(method, url, true); |
158 |
| - if (data instanceof Object) { |
159 |
| - data = JSON.stringify(data); |
160 |
| - request.setRequestHeader('Content-Type', 'application/json'); |
161 |
| - } |
162 |
| - request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
163 |
| - request.send(data); |
164 |
| - }; |
| 139 | +```javascript |
| 140 | +Lettuce.get = function (url, callback) { |
| 141 | + Lettuce.send(url, 'GET', callback); |
| 142 | +}; |
| 143 | + |
| 144 | +Lettuce.load = function (url, callback) { |
| 145 | + Lettuce.send(url, 'GET', callback); |
| 146 | +}; |
| 147 | + |
| 148 | +Lettuce.post = function (url, data, callback) { |
| 149 | + Lettuce.send(url, 'POST', callback, data); |
| 150 | +}; |
| 151 | + |
| 152 | +Lettuce.send = function (url, method, callback, data) { |
| 153 | + data = data || null; |
| 154 | + var request = new XMLHttpRequest(); |
| 155 | + if (callback instanceof Function) { |
| 156 | + request.onreadystatechange = function () { |
| 157 | + if (request.readyState === 4 && (request.status === 200 || request.status === 0)) { |
| 158 | + callback(request.responseText); |
| 159 | + } |
| 160 | + }; |
| 161 | + } |
| 162 | + request.open(method, url, true); |
| 163 | + if (data instanceof Object) { |
| 164 | + data = JSON.stringify(data); |
| 165 | + request.setRequestHeader('Content-Type', 'application/json'); |
| 166 | + } |
| 167 | + request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
| 168 | + request.send(data); |
| 169 | +}; |
| 170 | +``` |
| 171 | + |
165 | 172 |
|
0 commit comments