2
votes

We have a JSON string of articles, where each article has comments. Also, both the article and the comments contain the author/commenter information.

Because Sencha is using "id" as a model key, after Sencha loads the JSON (as shown below) the first article contains article.user but its comment objects do not contain it, e.g. comment.user does not exist. The question is how can we avoid that issue?

One approach we tried is to remove the id field from the user model and the JSON. Then, Sencha will create its own id values and in that case all the data is loaded. However, it seems that if we made a change to the article.user object we wouldn't see that change in the other user objects that (we know) correspond to the same user.

Another approach which would save bandwidth and solve the previous problem is to avoid having (potentially) multiple copies of the same user object by having a pointer to the object for those cases. In this case, the user object of the article would be loaded but the other instances would only contain a pointer to that object. The problem here is that we cannot remove the initial object.

What would be the standard way to approach this problem in Sencha Touch (2)?

{"articles": [
    {
        "id": "14338138",
        "user_id": "1",
        "title": "test",
        "user": {
            "id":"1545"
            "first_name": "Joe",
            "last_name": "Kae",
            "status":"1"
        },
        "comments": [
            {
                "id": "1545",
                "article_id": "14338138",
                "says":"This is my first comment to my own article.",
                "user": {
                    "id":"42",
                    "first_name": "Joe",
                    "last_name": "Kae",
                    "status": "1"
                }
            },
            {
                "id": "1546",
                "article_id": "14338138",
                "says":"This is my second comment to my own article.",
                "user": {
                    "id":"42",
                    "first_name": "Joe",
                    "last_name": "Kae",
                    "status": "1"
                }
            }
       ]
    }
]
});
1

1 Answers