0

I am working on an application in Backbone.js and Jquery which requires a JSON to be like this:-

{
      "FolderSummaryResponse": {
        "Status": "SUCCESS",
        "FolderItem": [
          {
            "Desc": "Immediate Action Required",
            "ID": "1",
            "RecordCnt": "0",
            "SeqCd": "1",
            "SubFolderItemList": {
              "FolderItem": [
                {
                  "Desc": "Customers to Call",
                  "ID": "10",
                  "RecordCnt": "0",
                  "SeqCd": "0"
                },
                {
                  "Desc": "PO Connect",
                  "ID": "17",
                  "RecordCnt": "0",
                  "SeqCd": "7",
                  "SubFolderItemList": {
                    "FolderItem": [
                      {
                        "Desc": "Inbound",
                        "ID": "73",
                        "RecordCnt": "0",
                        "SeqCd": "0"
                      },
                      {
                        "Desc": "Outbound",
                        "ID": "74",
                        "RecordCnt": "0",
                        "SeqCd": "1"
                      }
                    ]
                  }
                }
 ]
  }
    }

. Basically a parent and children kind of structure. How can I a create a Model and collection for this in backbone? Please help me as I am completely new to backbone.js .

  • is it possible for you to change the JSON response from server? You'll need to write a fair bit of code to make the above work seamlessly with Backbone. – neebz Mar 18 '13 at 18:56
  • this is a sample response, number of FolderItems and SubFolderItemList will vary for different conditions. Can you please let me know if there is any way where I can create a model having colletion of its own. – ashwin74268 Mar 18 '13 at 19:03
  • yes you can but you would be better off if your JSON response is something like `[{ Desc: "PO Connect", subFolderList: [ {}, {} , {} ] }, { }, { }]` - this would be far easier to parse for Backbone as compared to the above response. Is it possible for you to change server code? – neebz Mar 18 '13 at 19:13
  • unfortunately, I cannot change the response, I thought of creating nested models but that did not worked. Response woluld be like this: {"Parent" { "ID" : "some value", "Desc" ; "some value 1", "children" : {"Parent" : [{"ID" : "some value2", "Desc" : "some value2" },{"ID" : "some value3", "Desc" : "some value3"}]} } – ashwin74268 Mar 18 '13 at 19:40

1 Answers1

0

There is a JavaScript library that sits on top of Backbone.js that reduces the amount of boiler plate code you have to write and they have a CompositeView that works best with nested collections. I've included the documentation and an article that discusses using it.

Marionette CompositeView documentation

Marionette CompositeView Article

Kalpers
  • 658
  • 5
  • 11