gpt4 book ai didi

javascript - parse.com 中的当前用户 [javascript]

转载 作者:行者123 更新时间:2023-11-30 17:05:11 26 4
gpt4 key购买 nike

我试过很多版本,就是不知道答案。我已经查看了文档,但还不够了解以使其正确。我正在尝试让用户使用解析登录网站。我可以让用户登录,但无法保留当前用户数据。一旦您返回,用户必须再次登录。我知道您可以为此使用当前用户,但我无法让它工作。

$(function() {

Parse.$ = jQuery;

// Replace this line with the one on your Quickstart Guide Page
Parse.initialize("parseID",
"javascriptID");
init();
});
function init()
{
currentUser = Parse.User.current();
loginStatus();
} // init

function loginStatus()
{

// var currentUser = Parse.User.current();
if (currentUser) {
// do stuff with the user
new blogs();
} else {
// show the signup or login page
new LoginView();
// }

LoginView = Parse.View.extend({
template: Handlebars.compile($('#login-tpl').html()),
events: {
'submit .form-signin': 'login'
},
login: function(e) {

// Prevent Default Submit Event
e.preventDefault();

// Get data from the form and put them into variables
var data = $(e.target).serializeArray(),
username = data[0].value,
password = data[1].value;

// Call Parse Login function with those variables
Parse.User.logIn(username, password, {
// If the username and password matches
success: function() {
blogs();
},
// If there is an error
error: function(user, error) {
console.log(error);
}
});
},
render: function(){
this.$el.html(this.template());
}
});

function blogs() {

// var user = Parse.User.current();


var Blog = Parse.Object.extend("Post");
var Blogs = Parse.Collection.extend({
model: Blog,
query: (new Parse.Query(Blog)).equalTo("author", Parse.User.current())
});
var BlogsView = Parse.View.extend({
template: Handlebars.compile($('#blogs-tpl').html()),
render: function(){
var collection = { blog: this.collection.toJSON() };
this.$el.html(this.template(collection));
}
});

var blogs = new Blogs();

blogs.fetch({
success: function(blogs) {
var blogsView = new BlogsView({ collection: blogs });
blogsView.render();
$('.main-container').html(blogsView.el);
}


})


};



// Render login view on page
// var loginView = new LoginView();
// loginView.render();
// $('.main-container').html(loginView.el);

最佳答案

尝试将 currentUser 变量设为全局变量。 currentUser 变量的值当前停留在 init() 函数的范围内。一旦您转到 loginStatus() 函数,currentUser 变量就会被重置。

尝试在给定代码之前实例化变量,如下所示。

//instantiate on the global scope
var currentUser;

$(function() {
Parse.$ = jQuery;

// Replace this line with the one on your Quickstart Guide Page
Parse.initialize("parseID", "javascriptID");
loginStatus();
});

function loginStatus() {
currentUser = Parse.User.current();
if(currentUser){
newBlogs();
}else{
new LoginView();
}
}
....

关于javascript - parse.com 中的当前用户 [javascript],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28181191/

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