ロードしたテンプレートからの応答を保証するために ajax リクエストを取得しようとして立ち往生しています。
基本的に、コードを実行して遅延オブジェクトを返すと、テンプレート オブジェクトを取得する前に $.then() が呼び出されます。これは、最初の実行時にのみ発生します。
私はこれで髪を引き裂いています!
私のajax呼び出し:
var ajax = {
getTemplate: (function (id) {
/// <summary>
/// This method when used with $.Deferred fetches a html string
//// containing the template appliciable
/// to the the supplied id.
/// The ajax request object is cached upon the first request and is
/// returned by the cache
/// upon each subsequent request.
/// </summary>
/// <param name="id" type="String">
/// The id that matches the filename to request the template object
/// from.
/// </param>
/// <returns type="jqXHR">
/// A superset of the XMLHTTPRequest object that impliments the
/// promise interface.
/// </param>
var cache = {}; // private cache
// Gets assigned to getTemplate.
return function (id) {
var url = "/templates/" + id + ".tpl";
return cache[id]|| $.ajax({
url: url,
dataType: "html",
success: function (data) {
//ajax.getTemplate(id, data);
cache[id] = data;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
log("template request " + id,
XMLHttpRequest,
textStatus,
errorThrown);
}
});
};
} ())
};
私はこのように私のメソッドでこれを呼び出しています:
$.when(ajax.getTemplate("tooltip-attributes")()).then(function (template) {
if (template) {
// Do my template stuff here.
}
});