Re: Best way to build and post multi nested Json

1,314 views
Skip to first unread message
Message has been deleted

James Wright

unread,
Sep 17, 2012, 3:56:59 PM9/17/12
to ang...@googlegroups.com
try and think of what the information will be on the page, then design your model to that... you can have larger objects living back in some service if you need to and then you can extract smaller parts and serve it out the controller.

if you get used to db's like mongo you will start to think in very large objects and it is easier to work with in many cases.

in your example i would just pass comments as an object literal inside an array:

var article = {
     text: "this is my article text blah blah",
     date: "some timestamp",
     author: "joe cool",
     comments: [
          { text: "my first comment", author: "will smith" },
 { text: "my first comment", author: "will smith" }, 
]
};

jamey

On Monday, September 17, 2012 2:33:24 PM UTC-4, dreamer wrote:
I am trying to build and post($http) some complicated multi nested Json and I just want to know how you guys are doing..
Is there some better way to design the model?  what if all below stuffs have to be sent together?

$scope.obj1 = 
{what:'learn angular', done:true, comments:''}
{what:'build an angular app', done:false, comments:''}];

//Nested to obj1
$scope.comments = 
{col1:'test', col2:'test'}
{col1:'test1', col2:'test2'}];

$scope.obj2 = 
{who:'me', when:'now'}
{who:'you', when:'yesterday'}];


Michael Bielski

unread,
Sep 17, 2012, 4:18:38 PM9/17/12
to ang...@googlegroups.com
In general I concur, but I think that your comments should be nested like this:

var article = {
    "text": "this is my article text blah blah",
    "date": "some timestamp",
    "author": "joe cool",
    "comments": [
        {
            "comment": {
                "text": "my first comment",
                "author": "billy smith"
            }
        },
        {
            "comment": {
                "text": "my first comment",
                "author": "will smith"
            }
        }
    ]
};

Both are valid JSON, but with this format you can address the comments individually, such as:

alert(article.comments[1].author);
//alert should show "will smith"
Message has been deleted

James Wright

unread,
Sep 18, 2012, 7:41:42 AM9/18/12
to ang...@googlegroups.com
Actually when i pasted yours in a fiddle, I have to do this to see the author:
alert(article.comments[1].comment.author); 


i prefer to have it less verbose.

James Wright

unread,
Sep 18, 2012, 7:42:56 AM9/18/12
to ang...@googlegroups.com
so dreamer, I would say do whatever you have to do on the client to make it work. match the view(model) to the page. when you pass the data back to the server once you convert it from json to native (c#, ruby, php etc) you can manipulate it however you like and the put it into the format it needs to be in.

jamey

On Monday, September 17, 2012 7:45:48 PM UTC-4, dreamer wrote:
Thanks for reply.. What if I need to map the info to relational model that's already at the server side?
We already know that screen design cannot be mapped to relational model in many cases..
I am thinking about the way to remap Json frontend screen info to the objects generated to relational model at the backend.. 

Any advice?
Reply all
Reply to author
Forward
0 new messages