0
votes

I am a newbie to Backbone and need help to get and set the models from a nested Json Structure using Backbone

I have a simple model and collection below

Model:

var PmaslDataModel = Backbone.Model.extend({ });

Collection:

app.PmaslDataCollection = Backbone.Collection.extend({
          url APPLICATION_URL+'aslview/getformatedDataRows',
          model         : PmaslDataModel 
    });

Initialize the collection

var pmaslDataCollection =   new app.PmaslDataCollection();

Fetching the above collection returns the data in the Json Format below

{
"id"    : "1",
"rowid" : "1",
"group" : "10__",
"datarows": [
             {
               "id": "sspvk_code",
               "default_display": "1",
               "order": "1",

            },
            {
               "id": "sspvk_code2",
               "default_display": "0",
               "order": "0",
           },
           {
               "id": "sspvk_code3",
               "default_display": "1",
               "order": "0",
           }
         ]
},
{
   "id"    : "2",
   "rowid" : "2",
   "group" : "11__",
   "datarows": [
                {
                  "id": "sspvk_code",
                  "default_display": "1",
                  "order": "1",

                },
                {
                   "id": "sspvk_code2",
                   "default_display": "0",
                   "order": "0",
                },
                {
                   "id": "sspvk_code3",
                   "default_display": "1",
                   "order": "0",
                }
            ]
}

I have to use the above structure to populate the values in a table where we have the row ids and the column values in the row appears in the datarows attributes

I have 2 Questions

  1. How can I set the "default_display" value to 0 for all the datarows with id=sspvk_code ?
  2. If I have a particular row id how can I fetch the datarows for that row,pass it to a underscore template and populate the template (that is ,in the template I need to access it with model.get('attributename value')
2

2 Answers

0
votes

1) To initially set default_display to 0 to all datarows with id "sspvk_code" (is that what you want?) you can use parse method like this I would say:

app.PmaslDataCollection = Backbone.Collection.extend({
  url   : APPLICATION_URL+'aslview/getformatedDataRows',
  model : PmaslDataModel,

  parse : function (response) {
    // ititerate through entire collection
    _.each(response, function (row) {
      // find datarow with sspvk_code id and set default_display to 0
      _.each(row.datarows, function (datarow) {
        if (datarow.id === "sspvk_code") {
          datarow.default_display = 0;
        }
      })
    })

    return response;
  }
});

2) What you mean by "fetch datarows"? Your JSON looks like you already have datarows in collection.

0
votes

2.If I have a particular row id how can I fetch the datarows for that row,pass it to a underscore template and populate the template (that is ,in the template I need to access it with model.get('attributename value')?

Once you fetch the collection, json data will be set as models in collection. So from the collection, you can pick the model with specified row id using Collection's findWhere method.

In your case, inside your collection,

this.findWhere({rowId:2});

1.How can I set the "default_display" value to 0 for all the datarows with id=sspvk_code ?

I guess this should be the server's responsibility to set default values instead of setting it here.