- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在为成员(member)或访客准备特定页面之前,我会检查用户是否已登录。如果他们有 cookie,他们就是成员(member),如果没有,他们就是客人。
我做得正确吗?
function check_if_member(){
var dfd = new $.Deferred();
if (readCookie('isco')){
//adds jquery data() element "user_information"
//after making a jquery ajax post to retrieve it
//and using a template to write data to the page
//with the `success()` callback
prepare_member('member');
} else {
//reveals some HTML on the page
prepare_member('guest');
}
}
$(document).ready(function(){
//before page loads, check if user is member
$.when(check_if_member())
.then(function(){
console.log($('body').data('user_information'));
return false;
});
});
我想我终于对延迟有了一些了解,但它们仍然让我感到困惑,我想知道我是否正确地构建了它,或者我是否需要为我的任何 ajax 请求添加解析或返回行或者将收集到的信息保存到 jquery data()
的行。谢谢。
prepare_member函数
function prepare_member(type) {
if (type == 'member') {
var user_information = readCookie('isco')
$('body').data('user_information', user_information);
var user_id = $('body').data('user_information').user_id;
$.ajax({
type: 'post',
url: "/address.php",
data: {
type: "retrieve",
user_id: user_id,
isbilling: true
},
dataType: 'json',
success: function (returnedData) {
$('body').data('user_information').billing_information = returnedData['address_0'];
//populate member billing fields
$('#member_billing_container').append('<div>').children(':last')
.vkTemplate('/member_billing_template.tmpl', returnedData, function () {
//some callback - possibly resolve promise
});
//populate member contact fields
$('#member_contact_container').append('<div>').children(':last')
.vkTemplate('/member_contact_template.tmpl', JSON.stringify(user_information), function () {
//some callback - possibly resolve promise
});
}
});
$('.guest_container, .guest').hide();
$('.member_container, .member').show();
} else {
$('.guest_container, .guest').show();
$('.member_container, .member').hide();
}
}
最佳答案
首先,由于我们讨论的是 Deferreds,因此我们不会使用 success
处理程序。 $.ajax()返回一个 promise ,因此我们可以将其链接到 done()并继续使用延迟模式。
然后,假设您希望在成员数据可用时立即解决您的 Deferred (这实际上使答案变得更简单,因为 vkTemplate()
不返回 promise ,并且我们可能必须编写一个包装器以保持代码可读)。
结果会是这样的:
function check_if_member()
{
var dfd = $.Deferred(), // Can be called without 'new'.
cookie = readCookie("isco"); // Avoid reading cookie twice.
prepare_member(cookie, dfd);
return dfd.promise(); // Return promise.
}
function prepare_member(user_information, dfd) {
if (user_information) {
// Member mode.
$("body").data("user-information", user_information);
$.ajax({
type: "post",
url: "/address.php",
data: {
type: "retrieve",
user_id: user_information.user_id,
isbilling: true
},
dataType: "json"
}).done(function(returnedData) {
user_information.billing_information = returnedData["address_0"];
dfd.resolve(); // Member data is available, resolve Deferred.
$("#member_billing_container").append("<div>").children(":last")
.vkTemplate("/member_billing_template.tmpl", returnedData,
function() {
// Maybe chain widgets or handlers...
});
$("#member_contact_container").append("<div>").children(":last")
.vkTemplate("/member_contact_template.tmpl",
JSON.stringify(user_information),
function () {
// Maybe chain widgets or handlers...
});
});
$(".guest_container, .guest").hide();
$(".member_container, .member").show();
} else {
// Guest mode.
dfd.resolve(); // No member data available, resolve Deferred
// (synchronously here). You might also want
// to reject() it instead.
$(".guest_container, .guest").show();
$(".member_container, .member").hide();
}
}
$(document).ready(function() {
$.when(check_if_member())
.then(function() {
console.log($("body").data("user-information"));
});
});
现在,也许您不必使用 <body>
元素的数据将用户信息传达给您的 then()
处理程序。延迟对象可以与任何信息一起解决(或拒绝或通知),例如:
user_information.billing_information = returnedData["address_0"];
dfd.resolve(user_information); // Member data is available, resolve Deferred
// and pass user information through promise.
那么你只需要写:
$(document).ready(function() {
$.when(check_if_member())
.then(function(user_information) {
console.log(user_information); // Will be undefined in guest mode.
});
});
关于jquery - 这种推迟是否需要某种解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15097601/
我想以与 Angular 的 defer.resolve 和 Javascript Promise 对象相同的方式解析一个 Promise 对象。换句话说:我想创建一个要返回的虚拟 promise 对
我有两个实时制作和更新的图,如下所示: 我使用标准函数 plot() 和 lines() 生成了这些图。由于我实时生成和重新生成这些图,因此需要进行大量重绘和闪烁操作。 是否可以在两个图都完成之前推迟
我的一个页面上有一些 js 代码,它使用 ajax 加载一些页面元素。这些发生在页面的最顶部,不幸的是减慢了页面其余部分的加载速度。我想知道是否有办法推迟此操作,以便它在页面加载后加载。 这是我正在使
我有以下场景。 CompletableFuture result = CompletableFuture.supplyAsync(task, executor); result.thenRun(()
我经常更新用户表,它只是设置用户的最后一次上线时间,我想知道是否有一种简单的方法可以延迟它们并将它们在短超时(5 分钟或所以)。这将大大减少对我的用户数据库的查询。 最佳答案 如果您执行UPDATE
Gtmetrix 说有一些来自 youtube 的 JS 文件需要推迟。我删除了 iframe 并使用 jQuery 和经典的 Javascript 代码推迟了它,但这两种方法都不起作用。 Gtmet
我正在寻找阻止切换 RadioButton 的可能性,但仍然捕获 Click 事件。不幸的是,使用 Enabled=false 或 IsHitTestVisible=false 属性会阻止 Click
在我的小应用程序中,我从系统(不受我控制)收到一系列排队的 onEventXXX()。 不能保证那些 onEventXXX() 的时间。唯一可以保证的是,它们会按照放入队列的顺序接收。 在 onEve
我有一个在多个 View Controller 中使用 UIWebViews 的应用程序。 UIWebViews 用于渲染本地生成的 html,无需缓慢的网络访问。 为了节省内存,我仅根据 viewc
有时我想推迟 Promise 构造函数的执行,但仍然能够定义 Promise 链。我发现并稍微调整了以下方法,但由于我对 promise 的经验很少,所以我想知道 是否有更简洁的方法来做到这一点? 这
我刚刚遇到了使用 jQuery 的 .delay() 方法(v 1.4.2)尝试延迟以下 .load() 方法的问题。 像这样: $('myElement').delay(500).load('myP
我定义了一个成员变量如下。由于变量不会传递,所以我决定在这里使用 scoped_ptr 而不是 shared_ptr。 class ClassName { public: ClassName(
在开发我的作品集时,我意识到页面加载需要很长时间,因为必须加载所有图像才能完成页面加载。 所以,我想知道是否有办法阻止或停止加载,然后再允许加载。 意思是,立即阻止加载,然后单击已加载的另一个元素,比
这个问题已经有答案了: Defer loading and parsing of PrimeFaces JavaScript files (2 个回答) 已关闭 8 年前。 Google pagesp
我正在尝试推迟或异步加载 Trip Advisor 小部件,但由于某种原因它不起作用。 我认为异步或延迟可能不起作用,因为 Trip Advisor 脚本随后执行 document.write 命令。
我正在尝试使用 Ajax 将一些内容加载到页面中。加载的 html 包含一些 JavaScript 代码。即使代码包装在 $(document).ready 中,它也会在内容插入文档之前执行,因为在加
我正在使用模板字符串来生成一些文件,我喜欢新的 f 字符串的简洁性,用于减少我以前的模板代码,如下所示: template_a = "The current name is {name}" names
我有一个处理多个客户端连接的 Android 绑定(bind)服务。如果我理解正确的话,如果没有客户端连接,Android 将终止该服务。 但是,此时,在客户端断开连接后 1 分钟内,我的服务可能仍有
我的以下目标是: 获取决议列表 (按顺序)扫描每个内容,找到第一个导致成功直播的内容 为了测试这一点,我有 testVideoPresence: var testCounter = 0; functi
现在,我让用户在注册页面上输入数据,其中包括让用户输入他们的“计划类型”。我将此用户数据存储在 Firebase 上。 在用户提交上一个输入页面后的下一页上,我将用户带到一个输出页面,该页面使用 An
我是一名优秀的程序员,十分优秀!