gpt4 book ai didi

javascript - 实现 AJAX GET 和自定义事件的 javascript 对象的语法

转载 作者:行者123 更新时间:2023-11-30 08:09:31 25 4
gpt4 key购买 nike

我有几个关于这个改编 self 发现的教程的小片段的问题 here .

var loader = (function ($, host) {
return {
loadTemplate: function (path) {
var tmplLoader = $.get(path)
.success(function (result) {
$("body").append(result);
})
.error(function (result) {
alert("Error Loading Template");
}) // --> (1) SEMICOLON?

// (2) How does this wire up an event to the previous
// jQuery AJAX GET? Didn't it already happen?
tmplLoader.complete(function () {
$(host).trigger("TemplateLoaded", [path]);
});
}
};
})(jQuery, document);
  1. 那里应该有一个分号吗?
  2. 似乎 AJAX GET 正在发生,然后一个事件正在连接到它 - 我在这里错过了什么?

最佳答案

Is there supposed to be a semicolon there?

它是可选的,但推荐使用。

It seems like the AJAX GET is happening and then an event is getting wired to it - what am I missing here?

AJAX 是异步的,因此请求在发送后立即完成的可能性很小。所以,是时候添加另一个回调了。即使没有,它仍然可以工作,因为 jQuery 使用 promises 实现了这些回调。 .参见 example here .

关于javascript - 实现 AJAX GET 和自定义事件的 javascript 对象的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12557684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com