parse.com中的currentuser [javascript]

时间:2015-01-27 22:25:21

标签: javascript parse-platform

我尝试了很多版本而且只是不知道答案。我查看了文档,但不知道是否正确。我正在尝试让用户使用解析登录网站。我可以让用户登录但是,无法保留当前的用户数据。一旦你回去,用户必须再次登录。我知道你可以使用当前用户,但我无法让它工作。

$(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);

1 个答案:

答案 0 :(得分:2)

尝试将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();
       }
}
....