0
我是backbone.js的初學者。我有一個模型的屬性和引用該模型的集合。我需要從後端獲取數據並使用這些數據填充模型。但我無法做到。如何使用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}
請幫我一把。
感謝@ David..I我能夠得到的模型集合。 – Mani
但我無法訪問模型使用索引即collectionObj.get(索引)...相反,每個模型的一些默認ID隱式設置(在我的情況c3,c4,c5 ..)n我必須使用collectionObj.get('c3')來訪問模型。如何設置或獲取每個模型的ID? – Mani
在您的模型中,您需要添加'idAttribute:customer_id',以便Backbone知道id屬性是什麼(默認爲'id')。 –