Javascript MVC automatically supports hierarchy of model objects

This is pretty cool...checkout the Associations section in this link and expand the Demo example source code to see an example
  http://javascriptmvc.com/docs.html#!jQuery.Model.static.attributes 

Each Contact has a list of Tasks and they defined tasks in attributes of the Contact Model. Because the json data comes back as an json array with property name "tasks" then jsmvc will automatically hydrate Task objects within each Contact into a "tasks" attribute
  ....then to get tasks for a contact object you say: var tasks = contact.attr('tasks');
 {'id': 2,'name' : 'Brian Moschel','birthday': '1983-11-10',
     tasks : [{id: 2, title: "write up funcunit", due: "2009-5-1"}, 
             {id: 3, title: "test funcunit", due: ......

That some pretty "strick slick" and is powerful OO support. If you have such an object hierarchy I'd recommend you follow this approach client side. Just make sure you get the json data from the server like the example shows because:
- for lists findAll jsmvc expects json within an array, if not returned that way it won't parse automatically e.g. [{id: 1, name : "justin"},{id:2, name: "brian"}, ...]
  (but if the json api does not return that way then you can override the models method to wrap the response in an array...I did for search)
- for a single model object find just return an plain object {id: 1, name : "justin"}

jsmvc will automajically set each json object property as an attribute into your Model with that property name which you can retrieve as follows:

Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more