Pot.js 1.21 and PotLite.js 1.38 released.
Pot.js 1.21 と PotLite.js 1.38 リリースしました。 Pot.js は非同期処理を中心とした JavaScript ライブラリです。 概要などの詳しい詳細は以下のリファレンスを参照ください。
Pot.js + PotLite.js - Document and Reference
Download
Pot.js+PotLite.js-zip
Changelog
ja:
Pot.Deferred コールバック内で FileReader を返したときの不具合を修正
JSDoc コメントが変だったのを修正
Blob コンストラクタと BlobBuilder の実装の違いを吸収する Pot.createBlob() 関数を実装
Pot.equals() に長さの違うオブジェクトを渡した時に不正な結果になる不具合を修正
jQuery.ajax を Pot.Deferred 化する Pot.deferrizejQueryAjax() をスクリプトの読み込み順序に関係なく実行できるよう調整
Pot.serializeToQueryString() で Boolean をそのまま文字列化するよう変更
要素のリサイズ値を取得できる Pot.getResizeSize() を実装
DOM 要素が画面上に見えてるか判別する Pot.isElementInView() 関数を実装
オブジェクトを文字列でダンプした結果を返す Pot.dump() 関数を実装
既存コンストラクタのプロパティに不具合があったのを修正
prototype と共にコンストラクタを生成できる Pot.createConstructor() を実装
Pot.debug() のエラー版となる Pot.error() を追加
Image インスタンスかどうか判別する Pot.isImage() を実装
Pot.maybeDeferred() に jQuery.Deferred や JSDeferred など他の Deferred インスタンスを渡すと Pot.Deferred インスタンス化するよう改訂
Pot.Deferred コールバック内で Image インスタンスを返すと then() で onload、rescue() で onerror が拾えるよう改訂
en:
Fixed Deferred when returned FileReader with event callback.
Fixed JSDoc comment and return value.
Added Pot.createBlob() function for browser compatibility with BlobBuilder and Blob constructor.
Fixed bug: Pot.equals() will return true for objects that have different lengths.
Improved Pot.deferrizejQueryAjax() for jQuery plugin.
Changed Pot.serializeToQueryString() to handle as it is Boolean (true and false).
Added Pot.getResizeSize() function.
Added Pot.isElementInView() function.
Added Pot.dump() function.
Fixed the constructor property initialization.
Added Pot.createConstructor() function.
Added Pot.error() function.
Added Pot.isImage() function.
Improved Pot.maybeDeferred() to convert to an instance of Pot.Deferred from jQuery.Deferred and JSDeferred.
Improved Pot.Deferred callback to ensure an instance of Image loading.
Pot.createBlob() は、ブラウザ環境に Blob が実装されていたら Blob を、BlobBuilder が実装されていたら BlobBuilder を使って Blob を生成します。
Pot.begin(function() { var blob = Pot.createBlob('hoge', 'text/plain'); // 第2引数は任意 var reader = new FileReader(); reader.readAsText(blob); return reader; }).then(function(res) { Pot.debug(res); // 'hoge' });
Pot.serializeToQueryString() は、以前は true や false を空文字 '' と扱っていたのを、'true', 'false' として扱うようになりました。 これは、Pot.request() でのパラメータの指定でも同じです。 Pot.dump() は、オブジェクトなど任意の値をダンプした文字列を返します。 参照は #1 などの表現になります。
var reg = /^[a-z]+$/g; var err = new Error('error!'); var str = new String('hello'); var arr = [1, 2, 3, {a: 4, b: 5, c: true}, false, null, void 0]; var obj = { key1 : 'val1', key2 : 'val2', arr : arr, arr2 : arr, strs : [str, str], err : err, err2 : err, reg1 : reg, reg2 : reg, reg3 : reg }; obj.obj = obj; Pot.debug( Pot.dump(obj) ); // #0 { // key1: "val1", // key2: "val2", // arr: #3 [ // 1, // 2, // 3, // { // a: 4, // b: 5, // c: true // }, // false, // null, // undefined // ], // arr2: #3, // strs: [ // #5 (new String("hello")), // #5 // ], // err: #6 (new Error("error!")), // err2: #6, // reg1: #8 (new RegExp(/^[a-z]+$/g)), // reg2: #8, // reg3: #8, // obj: #0 // }
Pot.createConstructor() は、prototype と共にコンストラクタを生成して返します。 {Function} Pot.createConstructor([name ,] proto [, init]); name が指定されると、toString に使われます。 init は初期化する関数名または関数を任意で指定します。 引数 init が省略されて、proto 内に 'init' というメソッドがあると、それを初期化する関数と扱います。
// proto 内に init で初期化メソッドを指定 var Hoge = Pot.createConstructor('Hoge', { init : function(a, b, c) { this.value = a + b + c; }, getHoge : function() { return 'hogehoge'; } }); Pot.debug(new Hoge(1, 2, 3).value); // 6 Pot.debug(new Hoge().getHoge()); // 'hogehoge'
// 初期化関数を関数で指定 var Fuga = Pot.createConstructor({ value : 1, addValue : function(v) { this.value += v; return this; }, getValue : function() { return this.value; } }, function(a, b, c) { this.value += a + b + c; }); Pot.debug(new Fuga(1, 2, 3).value); // 7 Pot.debug(new Fuga(1, 2, 3).addValue(10).getValue()); // 17
// initialize という名前で初期化関数を指定 var Piyo = Pot.createConstructor('Piyo', { initialize : function(a, b, c) { this.value = a + b + c; }, getValue : function() { return this.value; } }, 'initialize'); Pot.debug(new Piyo(10, 20, 30).getValue()); // 60
Pot.maybeDeferred() は、jQuery.Deferred や JSDeferred など、他のライブラリの Deferred インスタンスを Pot.Deferred インスタンス化できるようになりました。
// jQuery.Deferred を Pot.Deferred 化 var d = $.Deferred(); d.done(function() { Pot.debug(1); }).done(function() { Pot.debug(2); }); // Pot.Deferred インスタンス化 Pot.maybeDeferred(d).then(function() { Pot.debug(3); }).ensure(function() { Pot.debug(4); }); d.resolve(); // output: // 1 // 2 // 3 // 4
JSDeferred と値の受け渡し
// JSDeferred インスタンス var d = new Deferred().next(function(res) { Pot.debug(res); // 1 return res + 1; }); // Pot.Deferred インスタンス化 Pot.maybeDeferred(d).then(function(res) { Pot.debug(res); // 2 return res + 1; }).ensure(function(res) { Pot.debug(res); // 3 }); d.call(1);
FileReader と同じく Pot.Deferred コールバック内で Image インスタンスを返すと then() で onload、rescue() で onerror が拾えるようになりました。
Pot.begin(function() { var img = new Image(); img.src = 'http://api.polygonpla.net/img/logo/pot.js.mini.png'; return img; }).then(function(img) { // ロード完了時 (onload) Pot.debug(img.width); // 300 }).rescue(function(err) { Pot.debug(err); // エラー時 (onerror) });
詳しい詳細は以下のリファレンスを参照ください。
Reference
Pot.js + PotLite.js - Document and Reference
Repository
polygonplanet/Pot.js - GitHub











