I'm writing an inventory system using mostly the Kendo UI Grid to process the invoicing system. When I'm creating a new invoice I need to create the invoice first, and then add the line items to the invoice and save it. I'm creating my invoice through an AJAX request and then using the create method of the DataSource to add the line items. I'm able to submit the invoice through my API and get back a response from the server with the newly created object, however how do I get the newly created object to interact with my Grid? Specifically I need the ID of the new invoice to add to the InvoiceID field of each of the line items of my Grid.
My thinking is that the best place to create the Invoice is in the saveChanges event of the Grid. Due to the asynchronous nature of JavaScript, which is somewhat new to me, how do I gt the object? Here is my code:
$(
"#grid"
).kendoGrid({
dataSource: {
batch:
false
,
transport: {
create: {
url: ...
},
contentType:
"application/json"
,
type:
"POST"
},
parameterMap:
function
(data, operation) {
return
JSON.stringify(data);
}
},
schema: {
data:
"Data"
,
total:
"Total"
,
model: {
id:
"InvoiceID"
,
fields: {
InvoiceID: { editable:
"false"
, type:
"number"
},
ClientID: { type:
"number"
},
LineItemID: { type:
"number"
},
...
}
}
}
},
pageable:
true
,
toolbar: [
"create"
,
"save"
,
"cancel"
],
columns: [
{ ... },
{ command: [
"edit"
,
"destroy"
], width:
"300px"
}],
editable:
true
,
save:
function
(e) {
...
}
},
saveChanges:
function
(e) {
newInvoice = CreateInvoice();
}
});
});
});
function
CreateInvoice() {
invoice = {
ClientID: $(
'#ClientID'
).text(),
Status: 1,
...
};
$.ajax({
url:
'/api/invoice/'
,
type:
'POST'
,
data: JSON.stringify(invoice),
contentType:
"application/json"
,
});
};
The invoice is created successfully and I need to take the ID of that object and apply it to the line items of each row in the Grid. What is the best way of doing this?