I am trying the knockout js example of cart editor given here
I want to delete all the rows added by the user as soon as the java script alert comes. I thought setting the cartLine array to nothing would do the job but it didn't work. I tried this:
self.save = function() {
cartLine([])
var dataToSave = $.map(self.lines(), function(line) {
return line.product() ? {
productName: line.product().name,
quantity: line.quantity()
} : undefined
});
alert("THANK YOU FOR YOUR ORDER!");
};
But it didn't help. Please correct me if I am wrong. Thanks.
Update:
self.save = function() {
var dataToSave = $.map(self.lines(), function(line) {
return line.product() ? {
productName: line.product().name,
quantity: line.quantity()
} : undefined
});
self.lines([]);
self.addLine = function() { self.lines.push(new CartLine()) };
alert("THANK YOU FOR YOUR ORDER!");
self.addLine = function() { self.lines.push(new CartLine()) }; //didn't work
};
The lines
field of the Cart
object contains all cart lines. It's used to generate dataToSave
, then you can clear it:
self.save = function() {
var dataToSave = $.map(self.lines(), function(line) {
return line.product() ? {
productName: line.product().name,
quantity: line.quantity()
} : undefined
});
self.lines([]); // clear cart lines
alert("THANK YOU FOR YOUR ORDER!");
self.addLine(); // add a new line