Question

I am a beginner in backbone.js . I have a model with attributes and a collection referenced to that model. I need to fetch data from back end and populate model with those data. But I am not able to do it.

Data coming from server is :

[{"aaData":
  [
    {"customer_id":2,"bom_id":19,
      "bom_name":"Anonymous123",
      "bom_description":"Anonymous123",
      "last_modified":"2014-02-13 12:15:54.0",
      "bom_status":"d",
      "version":1,
      "pu_id":1},

     {"customer_id":3,"bom_id":20,
        "bom_name":"bom_idea_AU",
        "bom_description":"mfsBomIdea",
        "last_modified":"2014-02-13 12:16:46.0",
        "bom_status":"d",
        "version":1,
        "pu_id":2}
  ],
  "customerId":"",
"bomId":"",
"bomName":"",
"bomDescription":"",
"lastModified":"",
"version":"",
"bomStatus":""}
]

Code so far:

  var bomRecordModel = Backbone.Model.extend({
    defaults : {
                    customerId: "",
                    bomId: "",
                    bomName : "",
                    bomDescription: "",
                    lastModified: "",
                    version: "",
                    bomStatus: ""

                },

});
var bomRecordObj = new bomRecordModel();


var bomRecordCollection = Backbone.Collection.extend({
            model : bomRecordModel,
            url: "/easybom/easybom?opcode=6&pu_id=0"
});
var brcObject =  new bomRecordCollection(); 
brcObject.fetch();

Ideally it should return two models after fetching data

model1:

{"customer_id":2,
"bom_id":19,
"bom_name":"Anonymous123",
"bom_description":"Anonymous123",
"last_modified":"2014-02-13 12:15:54.0",
"bom_status":"d",
"version":1,
"pu_id":1}

model2:

{"customer_id":3,
"bom_id":20,
"bom_name":"bom_idea_AU",
"bom_description":"mfsBomIdea",
"last_modified":"2014-02-13 12:16:46.0","bom_status":"d",
"version":1,"pu_id":2}

Please help me out.

Was it helpful?

Solution

You need to aprse the response from your server:

var bomRecordCollection = Backbone.Collection.extend({
            model : bomRecordModel,
            url: "/easybom/easybom?opcode=6&pu_id=0",
            parse: function(response){
                return response.aaData;
            }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top