parse.com中的currentuser[javascript]
我试过很多版本,只是不知道答案。我已经看了文档,但知道的还不足以让它正确。我正试图让用户登录到网站使用解析。我可以让用户登录,但无法保留当前用户数据。一旦您返回,用户必须再次登录。我知道你可以用当前用户来做这个,但我就是不能让它工作parse.com中的currentuser[javascript],javascript,parse-platform,Javascript,Parse Platform,我试过很多版本,只是不知道答案。我已经看了文档,但知道的还不足以让它正确。我正试图让用户登录到网站使用解析。我可以让用户登录,但无法保留当前用户数据。一旦您返回,用户必须再次登录。我知道你可以用当前用户来做这个,但我就是不能让它工作 $(function() { Parse.$ = jQuery; // Replace this line with the one on your Quickstart Guide Page Parse.initialize("parseID",
$(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();
}
}
....
没问题。这当然是小事情,将得到你!