Permalink
Browse files

jsonp 追加

  • Loading branch information...
1 parent 2ad21bf commit 59ab01f1a0a53483ef038a4a856bf4ad33d6c7c0 @cho45 committed Jun 29, 2008
Showing with 150 additions and 18 deletions.
  1. +35 −2 binding/userscript.js
  2. +4 −4 doc/index.html
  3. +2 −3 jsdeferred.jquery.js
  4. +2 −3 jsdeferred.mini.js
  5. +4 −4 jsdeferred.nodoc.js
  6. +35 −2 jsdeferred.userscript.js
  7. +68 −0 userscript-test.html
View
@@ -57,8 +57,41 @@ function http (opts) {
d.xhr = req;
return d;
}
-http.get = function (url) { return http({method:"get", url:url}) };
-http.post = function (url, data) { return http({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}}) };
+http.get = function (url) { return http({method:"get", url:url}) };
+http.post = function (url, data) { return http({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}}) };
+http.jsonp = function (url, params) {
+ if (!params) params = {};
+
+ var Global = (function () { return this })();
+ var d = Deferred();
+ var cbname = params["callback"];
+ if (!cbname) do {
+ cbname = "callback" + String(Math.random()).slice(2);
+ } while (typeof(Global[cbname]) != "undefined");
+
+ params["callback"] = cbname;
+
+ url += (url.indexOf("?") == -1) ? "?" : "&";
+
+ for (var name in params) if (params.hasOwnProperty(name)) {
+ url = url + encodeURIComponent(name) + "=" + encodeURIComponent(params[name]) + "&";
+ }
+
+ alert(url);
+
+ var script = document.createElement('script');
+ script.type = "text/javascript";
+ script.charset = "utf-8";
+ script.src = url;
+ document.body.appendChild(script);
+
+ Global[cbname] = function callback (data) {
+ delete Global[cbname];
+ document.body.removeChild(script);
+ d.call(data);
+ };
+ return d;
+};
Deferred.Deferred = Deferred;
Deferred.http = http;
View
@@ -99,14 +99,14 @@
parallel([$.get("foo.html"), $.get("bar.html")]).next(function (values) {
log($.map(values, function (v) { return v.length }));
- if (values[1].match(/nextUrl:\s*([^\s]+)/)) {
+ if (values[1].match(/nextUrl:\s*(\S+)/)) {
return $.get(RegExp.$1).next(function (d) {
return d;
});
}
}).
next(function (d) {
- log(d.length)
+ log(d.length);
});
</pre>
@@ -357,7 +357,7 @@
}).
next(function (r) {
print([r, "end"]);
-})
+});
</pre>
</di>
@@ -383,7 +383,7 @@
<p>Sample:
</p>
<pre>//=> loop 1 to 100
-loop({begin:1, end:100,step:10}, function (n, o) {
+loop({begin:1, end:100, step:10}, function (n, o) {
for (var i = 0; i < o.step; i++) {
log(n+i);
}
@@ -38,14 +38,13 @@ for(var i in dl)if(dl.hasOwnProperty(i)){
dl[i].cancel();}
};return ret;};Deferred.wait=function(n){
var d=new Deferred(),t=new Date();var id=setTimeout(function(){
-clearTimeout(id);d.call((new Date).getTime()-t.getTime());},n*1000)
-d.canceller=function(){try{clearTimeout(id)}catch(e){}};return d;};Deferred.next=function(fun){
+clearTimeout(id);d.call((new Date).getTime()-t.getTime());},n*1000);d.canceller=function(){try{clearTimeout(id)}catch(e){}};return d;};Deferred.next=function(fun){
var d=new Deferred();var id=setTimeout(function(){clearTimeout(id);d.call()},0);if(fun)d.callback.ok=fun;d.canceller=function(){try{clearTimeout(id)}catch(e){}};return d;};Deferred.call=function(f,args){
args=Array.prototype.slice.call(arguments);f=args.shift();return Deferred.next(function(){
return f.apply(this,args);});};Deferred.loop=function(n,fun){
var o={
begin:n.begin || 0,
-end:n.end ||(n-1),
+end:(typeof n.end=="number")? n.end:n-1,
step:n.step || 1,
last:false,
prev:null
View
@@ -38,14 +38,13 @@ for(var i in dl)if(dl.hasOwnProperty(i)){
dl[i].cancel();}
};return ret;};Deferred.wait=function(n){
var d=new Deferred(),t=new Date();var id=setTimeout(function(){
-clearTimeout(id);d.call((new Date).getTime()-t.getTime());},n*1000)
-d.canceller=function(){try{clearTimeout(id)}catch(e){}};return d;};Deferred.next=function(fun){
+clearTimeout(id);d.call((new Date).getTime()-t.getTime());},n*1000);d.canceller=function(){try{clearTimeout(id)}catch(e){}};return d;};Deferred.next=function(fun){
var d=new Deferred();var id=setTimeout(function(){clearTimeout(id);d.call()},0);if(fun)d.callback.ok=fun;d.canceller=function(){try{clearTimeout(id)}catch(e){}};return d;};Deferred.call=function(f,args){
args=Array.prototype.slice.call(arguments);f=args.shift();return Deferred.next(function(){
return f.apply(this,args);});};Deferred.loop=function(n,fun){
var o={
begin:n.begin || 0,
-end:n.end ||(n-1),
+end:(typeof n.end=="number")? n.end:n-1,
step:n.step || 1,
last:false,
prev:null
View
@@ -77,16 +77,16 @@ Deferred.wait = function (n) {
var id = setTimeout(function () {
clearTimeout(id);
d.call((new Date).getTime() - t.getTime());
- }, n * 1000)
- d.canceller = function () { try { clearTimeout(id) } catch (e) {} };
+ }, n * 1000);
+ d.canceller = function () { try { clearTimeout(id) } catch (e) {} };
return d;
};
Deferred.next = function (fun) {
var d = new Deferred();
var id = setTimeout(function () { clearTimeout(id); d.call() }, 0);
if (fun) d.callback.ok = fun;
- d.canceller = function () { try { clearTimeout(id) } catch (e) {} };
+ d.canceller = function () { try { clearTimeout(id) } catch (e) {} };
return d;
};
@@ -101,7 +101,7 @@ Deferred.call = function (f, args) {
Deferred.loop = function (n, fun) {
var o = {
begin : n.begin || 0,
- end : n.end || (n - 1),
+ end : (typeof n.end == "number") ? n.end : n - 1,
step : n.step || 1,
last : false,
prev : null
@@ -201,8 +201,41 @@ function http (opts) {
d.xhr = req;
return d;
}
-http.get = function (url) { return http({method:"get", url:url}) };
-http.post = function (url, data) { return http({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}}) };
+http.get = function (url) { return http({method:"get", url:url}) };
+http.post = function (url, data) { return http({method:"post", url:url, data:data, headers:{"Content-Type":"application/x-www-form-urlencoded"}}) };
+http.jsonp = function (url, params) {
+ if (!params) params = {};
+
+ var Global = (function () { return this })();
+ var d = Deferred();
+ var cbname = params["callback"];
+ if (!cbname) do {
+ cbname = "callback" + String(Math.random()).slice(2);
+ } while (typeof(Global[cbname]) != "undefined");
+
+ params["callback"] = cbname;
+
+ url += (url.indexOf("?") == -1) ? "?" : "&";
+
+ for (var name in params) if (params.hasOwnProperty(name)) {
+ url = url + encodeURIComponent(name) + "=" + encodeURIComponent(params[name]) + "&";
+ }
+
+ alert(url);
+
+ var script = document.createElement('script');
+ script.type = "text/javascript";
+ script.charset = "utf-8";
+ script.src = url;
+ document.body.appendChild(script);
+
+ Global[cbname] = function callback (data) {
+ delete Global[cbname];
+ document.body.removeChild(script);
+ d.call(data);
+ };
+ return d;
+};
Deferred.Deferred = Deferred;
Deferred.http = http;
View
@@ -0,0 +1,68 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>JSDeferred Sample</title>
+ <script type="text/javascript" src="jsdeferred.js"></script>
+ <script type="text/javascript" src="binding/userscript.js"></script>
+ <script type="text/javascript">
+ // <![CDATA[
+
+ window.onload = function () {
+ with (D()) {
+
+ http.jsonp("http://wedata.net/databases/AutoPagerize/items.json").next(function (data) {
+ alert(data);
+ });
+
+ }
+ };
+
+ // ]]>
+ </script>
+
+ <style type="text/css">
+ body {
+ padding: 0 10% 10em;
+ margin: 0;
+ color: #333;
+ line-height: 1.5;
+ }
+
+ h2 {
+ margin: 3em 0 1.5em 0;
+ }
+
+ div.code,
+ pre {
+ font-size: 80%;
+ font-family: monospace;
+ background: #efefef;
+ border: 1px solid #ddd;
+ padding: 1em;
+ line-height: 1.33;
+ overflow: auto;
+ }
+
+ em,
+ span.em {
+ font-weight: bold;
+ font-style: normal;
+ color: #f00;
+ }
+
+ textarea {
+ width: 100%;
+ }
+
+ #footer {
+ margin: 2em 0;
+ text-align: right;
+ font-size: 80%;
+ }
+ </style>
+ </head>
+ <body>
+ <div id="whole">
+ </div>
+ </body>
+</html>

0 comments on commit 59ab01f

Please sign in to comment.