Hi i need to format my Collection in order to for my Rest to accept the request. ( I recreated my project so i already know specifically what format my REST accepts )
The format should be :
{"input":"{invoice:[{}]","InvoiceDetails:[{},{},{}]}}
So in my backbone project i have:
CartCollection //collection
InvoiceDetailsCollection //collection
Invoice //model
InvoiceDetail //model
Cart //model
This is my save function:
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()));
}
My Current Format is this
[{"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}}]
Nothing in this setup looks standard .
The way you manage the InvoiceDetailCollection
is not like a normal Collection
, looks more like a Model
with two Collections
into. A Collection is fed with a Model
or the Hash to build a Model not with this key/value pairs that doesn't match with the Collection.model
.
Then the URL you want to build has a magic root key called input
then one key invoice
(lowcase) which contains an array of only one element which is a Hash, then InvoiceDetails
(camelcase) which you are continuously reseting to the actual cart
content.
I think what you need is:
InvoiceDetailsCollection
: keeping all the carts Invoice
: the invoice Input
: the model that keeps both Invoice
and InvoiceDetails
Then:
Invoice
Model. InvoiceDetailsCollection
with your carts. Input.set( "invoice", invoice )
Input.set( "invoice_details", InvoiceDetailsCollection )
Overwrite the Input.toJSON
to returns input: this.toJSON()
(code simplified) .
But still I think your live could be easier if you could change this URL for something more REST. I think that all this info belongs to the Invoice
Model so you could accept a request like:
/invoices
{ "POSWorkstationID": "POS7", "POSClerkID": "admin", ... , "Details": [{}, {}] }
This way you can remove the Input
Model that I suggested to wrapper all the weird behavior and keep the InvoiceDetailsCollection
into the Invoice
Model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.