![](/img/trans.png)
[英]AngularJS/Parse.com : $scope.currentUser variable not updated
[英]currentuser in parse.com [javascript]
我尝试了很多版本,只是不知道答案。 我看过文档,但对它的了解还不够。 我正在尝试让用户使用解析登录网站。 我可以让用户登录,但是不能保留当前用户数据。 一旦您返回,用户就必须再次登录。 我知道您可以为此使用当前用户,但我无法使其正常工作。
$(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();
}
}
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.