如何使用backbone.js填充服务器数据模型

时间:2014-02-14 13:36:55

标签: jquery backbone.js backbone-events

我是backbone.js的初学者。我有一个带有属性的模型和一个引用该模型的集合。我需要从后端获取数据并使用这些数据填充模型。但我无法做到。

来自服务器的数据是:

[{"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":""}
]
到目前为止

代码:

  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();

理想情况下,它应该在获取数据后返回两个模型

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}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

您需要解析服务器的响应:

var bomRecordCollection = Backbone.Collection.extend({
            model : bomRecordModel,
            url: "/easybom/easybom?opcode=6&pu_id=0",
            parse: function(response){
                return response.aaData;
            }
});