如何仅在特定事件之后加载服务器变量
How to load a server variable only after a specific event?
我需要两个服务器变量加载后,只有在数据库的var变化。
现在,它们在加载页面时立即加载,因此保持事件(upvotesRef.transaction
)之前的值。
$('.HP').text("<%= post.upvotes - post.downvotes %> HP");
代码:
$('.UpvoteButton').click(function () {
var $this = $(this);
if ($this.hasClass("on")) {
$this.removeClass("on");
upvotesRef.transaction(function (upvotes) {
if (!upvotes) {
upvotes = 0;
}
upvotes = upvotes - 1;
return upvotes;
});
userRef.remove();
$('.HP').text("<%= post.upvotes - post.downvotes %> HP");
编辑:
我最终创建了局部变量来解决我的问题。谢谢你的回答!
var upvotesLocal = <%= post.upvotes %>;
var downvotesLocal = <%= post.downvotes %>;
$('.UpvoteButton').click(function () {
var $this = $(this);
if ($this.hasClass("on")) {
$this.removeClass("on");
upvotesRef.transaction(function (upvotes) {
if (!upvotes) {
upvotes = 0;
}
upvotes = upvotes - 1;
return upvotes;
});
upvotesLocal = upvotesLocal -1
userRef.remove();
$('.HP').text((upvotesLocal - downvotesLocal) + " HP")
您应该在事务后获取和更新变量,对吗?
这意味着它应该在回调中发生。
我假设您正在使用firebase事务方法。
然后你的回调应该作为第二个参数:
$('.UpvoteButton').click(function () {
var $this = $(this);
if ($this.hasClass("on")) {
$this.removeClass("on");
upvotesRef.transaction(function (upvotes) {
if (!upvotes) {
upvotes = 0;
}
upvotes = upvotes - 1;
return upvotes;
}, updateVotes); //<-- here the callback function added
userRef.remove();
function updateVotes(error, isCommitted, upvotes) {
//here you got already your new upvotes
//but you should request the downvotes,
//here you need the ajax request, see e.g. Dez's answer or comments
}
考虑到我在node.js方面没有经验,你应该做的是将$('.HP').text("<%= post.upvotes - post.downvotes %> HP");
行替换为AJAX调用,在那里你可以获得更新的变量并使用jQuery添加它们。下面的内容:
$('.UpvoteButton').click(function () {
var $this = $(this);
if ($this.hasClass("on")) {
$this.removeClass("on");
upvotesRef.transaction(function (upvotes) {
if (!upvotes) {
upvotes = 0;
}
upvotes = upvotes - 1;
return upvotes;
});
$.ajax({
type : 'POST',
data: { userRef: userRef},
contentType: 'application/json',
url: 'http://localhost:3000/endpoint',
success: function(data) {
console.log('success');
console.log(JSON.stringify(data));
/* Do whatever you need with your data, for example*/
$('.HP').text(data.upvotes + "-" + data.downvotes + "HP");
},
error : function (event) {
console.log(event);
}
});
userRef.remove();
...
}
在node.js
一侧,您将需要以下内容:
app.post('/endpoint', function(req, res) {
console.log(req.body.userRef)
/* Get the upvotes and downvotes and send them back in the response */
res.contentType('json');
res.send({ some: JSON.stringify({response:'json'}) });
});
我很抱歉不能给出一个准确的答案。
相关文章:
- 在chrome.tabs.onCreated之后加载HTML页面
- 在指令之后加载的角度ng-init函数
- 在Ajax(过滤器)加载之后加载jQuery脚本
- document.ready是在async js之后加载的
- JQuery在F5之后加载
- 如何在完全加载html之后加载jquery代码段
- Angularjs在初始json之后加载
- 在页面加载的所有其他文件之后加载动画 gif
- 如何在 document.ready() 之后加载的 HTML 上使用 jquery
- jqGrid:在表中已有的本地数据之后加载 JSON 数据
- 在 ExtJS 之后加载 jQuery
- 在jQuery getJSON之后加载PaperScript
- Wordpress强制脚本在插件之后加载
- 在第一部分完成ajax之后加载表单的第二部分
- 在其他脚本之前或之后加载html5shivJS有关系吗?(页面加载时间)
- 在包含ajax之后加载js文件
- 在XMLHttpRequest之后加载jQuery选项卡
- 如何仅在特定事件之后加载服务器变量
- Angular.js控制器在视图加载之后加载
- 如何在另一个已加载的iframe之后加载一个iframe