嗨,我需要格式化我的集合,以便我的Rest接受請求。 (我重建我的項目,所以我已經知道具體是我的休息接受什麼格式)backbone.js中的格式集合
格式應該是:
{"input":"{invoice:[{}]","InvoiceDetails:[{},{},{}]}}
所以在我的骨幹工程,我有:
CartCollection //collection
InvoiceDetailsCollection //collection
Invoice //model
InvoiceDetail //model
Cart //model
這是我的保存功能:
save: function(){
invoice = new Invoice();
invoice.set({POSWorkstationID: "POS7"});
invoice.set({POSClerkID: "admin"});
invoice.set({CustomerName: "Alice in Wonderland Tours"});
invoice.set({IsFreightOverwrite: true});
invoice.set({BillToCode: "CUST-000009"});
InvoiceDetailCollection.add([{Invoices: invoice}]);
_.each(this.collection.models, function(cart){
InvoiceDetailCollection.add([{InvoiceDetails: cart }]);
});
alert(JSON.stringify(InvoiceDetailCollection.toJSON()));
}
我的當前格式是這樣的
[{"Invoices":{"POSWorkstationID":"POS7","POSClerkID":"admin","CustomerName":"Alice in Wonderland Tours","IsFreightOverwrite":true,"BillToCode":"CUST-000009"}},{"InvoiceDetails":{"Qty":"1","ItemCode":"ITEM-000004","ItemDescription":"Old World Lamppost\r\n\r\n","RetailPrice":107.99,"SalesPrice":107.99}}]
InvoiceDetailsCollection:保留所有的購物車//其實我正在那樣做。只是我在添加購物車到InvoiceDetailCollection時添加了InvoiceDetails。 – jongbanaag 2012-03-28 10:46:15