如何在Parse上用不同的父对象ACL保存指针?

时间:2015-10-04 00:44:56

标签: javascript pointers parse-platform relationship acl

我这里有一个很难的问题;我现在至少看了三次Parse.com js SDK,但是无法解决这个问题。

我有一个带有文字,用户,评论的问题类。 我有一个带有文字的评论类,用户。

注释放在一个指向问题类的指针数组中。

Question = Parse.Object.extend('Question');
question = new Question();
question.set('text', 'my first question');

// the user posting the question
var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
question.setACL(acl);
question.set('user', Parse.User.current());
question.save();

这里一切都很好。现在另一个用户登录应用程序并发布评论

question; // let's asume the question is fetched
Comment = Parse.Object.extend('Comment');
comment = new Comment();
comment.set('text', 'my first comment on the first question');

// the user posting the comment (another user)
// this user has public read rights on the comment but not write rights
// he has the write rights on his comment though

var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
comment.setACL(acl);
comment.set('user', Parse.User.current());
comment.save();

// comment is saved we can now add it to the question's array
question.add(comment);
question.save(); // this is where everything fails... 

它说用户没有权利在问题对象上写这是正常的但如果我不用新数组保存问题,如何保存评论呢?

1 个答案:

答案 0 :(得分:1)

选项1:加入表

question; // let's asume the question is fetched

//GET OBJECT ID HERE WHEN FETCHING QUESTION
questionID = question.id;


Comment = Parse.Object.extend('Comment');
comment = new Comment();
comment.set('text', 'my first comment on the first question');

// the user posting the comment (another user)
// this user is have public read rights on the comment but not write rights
// he has the write rights on his comment though

var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
comment.setACL(acl);
comment.set('user', Parse.User.current());

//NEW CODE HERE TOO
comment.save().then(function(results){
  commentID = results.id;
});;

// NOW ADD COMMENT AND QUESTION TO JOIN TABLE BY OBJECTID
JoinTable = Parse.Object.extend('JoinTable');
entry = new JoinTable();
entry.set('questionID', questionID);
entry.set('commentID', commentID);
entry.save().then(function(results){
//MIGHT WANT TO PUSH THE NEW COMMENT ONTO THE COMMENTS ARRAY 
//WHICH YOU ARE DISPLAYING WITH YOUR QUESTION
});

现在,当你加载一个问题时,只需通过questionID对连接表进行查询,它将为你提供与问题相关的所有commentID。然后使用该commentID查询comments表,加载并显示这些结果。

选项2:通过评论保存问题ID

question; // let's asume the question is fetched

//GET OBJECT ID HERE WHEN FETCHING QUESTION
questionID = question.id;


Comment = Parse.Object.extend('Comment');
comment = new Comment();
comment.set('text', 'my first comment on the first question');

// the user posting the comment (another user)
// this user is have public read rights on the comment but not write rights
// he has the write rights on his comment though

var acl = new Parse.ACL(Parse.User.current()); 
acl.setPublicReadAccess(true);
comment.setACL(acl);
comment.set('user', Parse.User.current());
//SET QUESTION ID ON COMMENT
comment.set('parent', questionID);


//NEW CODE HERE TOO
comment.save().then(function(results){
  commentID = results.id;
});;

现在,当您查询问题时,还要查询评论表并使用当前问题objectID检索所有评论作为他们的父母'。

您在评论中提及

该解析不是关系型的。 Parse基于MongoDB,它是一个NoSQL数据库。但是,您可以通过引用来做关系。"

http://docs.mongodb.org/manual/core/data-modeling-introduction/

要回答两个问题和评论

//First get the question
var Question = Parse.Object.extend("Question");
var query = new Parse.Query(Question);
query.equalTo("questionID", questionID);
query.find({
  success: function(results) {
     //You will have to check results and see if it returns an array
     //If it does you will need to use results[0].id to select 1st
     //element in array. The follow code is if results is an Object

     var JoinTable = Parse.Object.extend("JoinTable");
     var query = new Parse.Query(JoinTable);
     query.equalTo("parentID", questionID);
     query.find({
        success: function(results) {
        //this should return an array, loop through the array to get
        //all of the IDs then, pe## Heading ##rform a compound query with all of 
        //IDs

      },
     error: function(error) {
        alert("Error: " + error.code + " " + error.message);
     }
    });
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

然而,写这个代码令人难以置信的烦恼和不必要因此我推荐选项两个,如何做

//First get the question
var Question = Parse.Object.extend("Question");
var query = new Parse.Query(Question);
query.equalTo("questionID", questionID);
query.find({
  success: function(results) {
     //You will have to check results and see if it returns an array
     //If it does you will need to use results[0].id to select 1st
     //element in array. The follow code is if results is an Object

    var questionID = results.id;

     var Comments = Parse.Object.extend("Comments");
     var query = new Parse.Query(Comments);
     query.equalTo("parentID", questionID);
     query.find({
        success: function(results) {
         //This returns your comments to be shared with your view

      },
     error: function(error) {
        alert("Error: " + error.code + " " + error.message);
     }
    });
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

您会注意到我已停止在commentID上的forEach循环中写出实际代码,因为我意识到它是多么不必要。希望这会有所帮助。