フォーマットで単一のグローバル変数を使用するようにリファクタリングされた webapp があります。
app.module.function
app.module.submodule.function
既存のジェネリックをリファクタリングしたい
function getData(id, type, url, successHandler, rel) {
var func = window[successHandler]
$.ajax({
type : "POST",
url : url,
data : {"search" : id, "type" : type, "rel" : rel},
dataType: "json",
success : function(data) {
if (func && typeof func === "function") {
func(data, id, 0);
}
}
});
}
関数を使用して、渡される成功ハンドラーを利用します。たとえば、そのような成功ハンドラーの 1 つが ですclientRelationshipHandler
。
ハンドラーが次のように定義されている場合
function clientRelationshipHandler(results, id, type) { .. }
次にwindow["clientRelationshipHandler"]
、関数を返します。
ただし、次のように変更すると
app.module.submodule.clientRelationshipHandler = function(results, id, type { .. }
両方
window["clientRelationshipHandler"]
window["app;.module.submodule.clientRelationshipHandler"]
未定義を返すため、一般的なgetData
機能が壊れます。特定のオブジェクトにバインドされた関数を使用しているときに、Ajax クエリの一般的な成功ハンドラーを実装するにはどうすればよいですか?