Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using parse.com and JavaScript SDK.

I've read alot about this, but cannot find any examples that fit my issue I'm trying to solve.

Here is what I'm attempting to achieve.

I want class "FriendRequest" to have a one to many relation with "myBadges". The child objects held in "myBadges" will be increasing overtime.

The child objects are generated by a function that runs when the user selects a badge and uses "Yes, do it now!" to store it in "myBadges".

At the moment I can only seem to create a relationship within the active class that the function uses. This means I have a relation set up in "myBadges" that just points to the User class, not to the class "FriendRequest".

The relation is in this part of code

                success: function(results) {
                        // The object was saved successfully.
                        userbadges.relation('BadgeConnect').add(userbadges);
                        userbadges.save();

So the problem I'm trying to solve is to move the relation link from the "myBadges" class to the "FriendRequest" class???

An example answer how how to achieve this would be great to help me learn the correct approach.

FULL CODE.

<script type="text/javascript">
        Parse.initialize("xxxx", "xxxxx");
        var MyBadges = Parse.Object.extend("myBadges");
        var friendRequest = Parse.Object.extend("FriendRequest");

        var userbadges = new MyBadges();
        var friendRequest = new friendRequest();
        var user = Parse.User.current();


        $(document).ready(function() {

            $("#send").click(function() {

                var badgeselected = $('#badgeselect .go').attr("src");
                userbadges.set("BadgeName", badgeselected);
                userbadges.set("uploadedBy", user);
                //userbadges.set('BadgeName');
                //userbadges.save();

                userbadges.save(null, {
                    success: function(results) {
                        // The object was saved successfully.
                        userbadges.relation('BadgeConnect').add(userbadges);
                        userbadges.save();
                        //location.reload();
                    },
                    error: function(contact, error) {
                        // The save failed.
                        // error is a Parse.Error with an error code and description.
                        alert("Error: " + error.code + " " + error.message);
                    }
                });
            });
        });
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Like most problems there are many ways to solve it.

If your goal is to be able to query this relationship from either side then I see two main options:

  • Create a Cloud Function for after-save of your myBadges class that creates a duplicate of the relation on the FriendRequest side. I'm not sure how much support there is for this as I've never tried it.
  • Create your own relationship class, e.g. myBadges_FriendRequest, inside it have a pointer to each of the other classes, plus anything else you might want to know about that relationship

Personally I recommend the 2nd option. For querying you can easily request information based on either side of the relationship and use include() to populate both pointers in the result as needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.