当前用户在 parse.com [JavaScript]

currentuser in parse.com [javascript]

本文关键字:JavaScript com parse 用户      更新时间:2023-09-26

我已经尝试了很多版本,只是不知道答案。我已经查看了文档,但没有足够的知识来正确处理它。我正在尝试让用户使用解析登录网站。我可以让用户登录,但无法保留当前用户数据。 一旦您返回,用户必须再次登录。 我知道您可以使用当前用户,但我无法让它工作。

$(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 变量的值当前停留在 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();
       }
}
....