Array に関数を追加できるように、(TypeScript を使用して) Array をサブクラス化しました。以下のコードを参照してください。
その後、{for}JsRender/JsViews のタグが機能しなくなっていることに気付きました。また、JsRender/JsViews が配列を反復処理するために使用していることにも気付きました。これは、サブクラス化された配列$.isArray(...)を返します。false
JsRender/JsViews コードを使用するように変更する(data instanceof Array)と、機能します。
JsRender/JsViews{for}タグを使用してサブクラス化された配列を取得する別の方法 (JsRender/JsViews コードを変更せずに) はありますか?
タイプスクリプト:
module MyModule {
export class Collection<T> implements Array<T> {
constructor() {
Array.apply(this, arguments);
return new Array();
}
length: number;
toString(): string { return ""; }
//... All other Array properties/methods
}
// replace dummy declarations with the real thing
Collection['prototype'] = new Array();
}
生成された Javascript:
var MyModule;
(function (MyModule) {
var Collection = (function () {
function Collection() {
Array.apply(this, arguments);
return new Array();
}
Collection.prototype.toString = function () { return ""; };
//... All other Array properties/methods
return Collection;
})();
MyModule.Collection = Collection;
// replace dummy declarations with the real thing
Collection['prototype'] = new Array();
})(MyModule|| (MyModule= {}));