在knockout.js购物车编辑器中清除行的例子

问题描述 投票:0回答:1

我正在尝试使用knockout js的购物车编辑器的例子。此处

我想在java脚本提示出现时,立即删除用户添加的所有行。我以为把cartLine数组设置为nothing就可以了,但是没有成功。我试过这个方法。

    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!");  
};

但是没有用 如果我说的不对,请纠正我。谢谢。

更新:我正在尝试使用knockout js的例子。

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
};
javascript knockout.js cart
1个回答
1
投票

lines 的领域 Cart 对象包含所有的购物车线。它用于生成 dataToSave,然后你就可以清除它。

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
© www.soinside.com 2019 - 2024. All rights reserved.