Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

How do I have a model deal with a JSON object that has two datasets in it?

$
0
0

I'm trying to create a "comment" model. The comment JSON returns like this:

{
    "comments": [
        {
            "__v": 0,
            "_id": "548b5f7fa48b89182c00006d",
            "by": "user",
            "changedBy": "51e6db56715ee2153800026b",
            "changedOn": "2014-12-12T21:34:55.308Z",
            "createdBy": "51e6db56715ee2153800026b",
            "createdOn": "2014-12-12T21:34:55.308Z",
            "feedback": "5320671f160a29055000014f",
            "status": 1,
            "text": "starting to see a difference! ",
            "type": "status"
        },
        {
            "__v": 0,
            "_id": "548b5e9ba48b89182c000069",
            "by": "user",
            "changedBy": "51e6db56715ee2153800026b",
            "changedOn": "2014-12-12T21:31:07.993Z",
            "createdBy": "51e6db56715ee2153800026b",
            "createdOn": "2014-12-12T21:31:07.992Z",
            "feedback": "5320671f160a29055000014f",
            "status": 0,
            "text": "Definitely could use work",
            "type": "status"
        },
        {
            "__v": 0,
            "_id": "5320671f160a290550000153",
            "by": "coach",
            "changedBy": "51e6db56715ee2153800026b",
            "changedOn": "2014-03-12T13:54:39.616Z",
            "createdBy": "51e6db56715ee2153800026b",
            "createdOn": "2014-03-12T13:54:39.615Z",
            "feedback": "5320671f160a29055000014f",
            "type": "add"
        }
    ],
    "users": {
        "51e6db56715ee2153800026b": {
            "_id": "51e6db56715ee2153800026b",
            "displayName": "Clayton Gray",
            "email": "claytonjgray@gmail.com",
            "firstName": "Clayton",
            "lastName": "Gray",
            "relationships": [
                {
                    "_id": "53173bc2c985a60000000034",
                    "id": "53173bc2c985a60000000034",
                    "label": "coach",
                    "user": "51573b7e2a66ee620200001c"
                }
            ],
            "thumbnail": "5368ef71c473347cd5000022",
            "title": "Local Account"
        }
    }
}
In my model, I'm trying to combine the user and the comment info into the model object. Currently, this is my code for parse:
parse : function(_resp) { 
        var model = this;
 
        _.each(_resp.comments, function (comment) {
          model.text = comment.text;
          model.createdBy = comment.createdBy;
        });
 
        var user = _.where(_resp.users, {"_id":model.createdBy})[0];
 
        model.user = user;
        model.displayName = model.user.displayName;
 
          // save the posts, need to get the post attributes
          return _resp.comments;
      }
Inside of parse, model.user exists. I can see it. But from anywhere outside of the that function, my model does not have a .user attribute. What am I missing?

Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>