knockout js observable array删除所有项目

时间:2014-04-06 18:26:35

标签: knockout.js

我是淘汰赛的新手。我试图从knockout observable数组中删除一个项目。但remove函数正在删除数组中的所有项。有人可以帮我吗?

以下是我的viewmodel的样子

function ReservationsViewModel() {
var self = this;
 self.availableMeals = ko.observableArray([
    { mealName: "Standard (sandwich)", price: 0 },
    { mealName: "Premium (lobster)", price: 34.95 },
    { mealName: "Ultimate (whole zebra)", price: 290 }
]);    


self.seats = ko.observableArray([]);


self.addSeat = function() {
    self.seats.push(self.availableMeals());
}
self.removeSeat = function(seat) { self.seats.remove(seat) }
}

ko.applyBindings(new ReservationsViewModel());

这是js小提琴

http://jsfiddle.net/pQN32/

谢谢,

普利文。

2 个答案:

答案 0 :(得分:1)

每次调用self.availableMeals()时,都会返回相同的数组对象。不是具有相同属性和值的对象,而是相同的对象。这意味着self.seats包含同一对象的多个副本。

Knockout的remove函数删除了===所有传入参数的项目,在这种情况下意味着它将删除所有项目源数组,因为它们都是相同的。

由于您没有在任何地方使用实际推送值,您只需按下虚拟值:

self.addSeat = function() {
    self.seats.push({});
}

答案 1 :(得分:1)

正如@DCoder所说,每次都会向数组添加相同的对象。 remove函数删除数组中的所有重复对象。

您的代码只是您尝试的代码的一半。

为了你开始,这里有一个更新的jsfiddle做你想要的(我想)。

http://jsfiddle.net/pQN32/5/

顺便说一句,如果您的availableMeals永远不会改变,那么您就不需要ko.observableArray

<强> HTML

<h2>Your seat reservations (<span data-bind="text: seats().length"></span>)</h2>

<table>
    <thead><tr>
        <th>Passenger name</th><th>Meal</th><th>Price</th><th>Surcharge</th><th></th>
    </tr></thead>
    <tbody data-bind="foreach: seats">
        <tr>
            <td data-bind="text: name"></td>
            <td><select data-bind="options: $root.availableMeals, optionsText: 'mealName', value: meal"></select></td>
            <td data-bind="text: formattedPrice"></td>
            <td></td>
            <td><a href="#" data-bind="click: $root.removeSeat">Remove</a></td>

        </tr>    
    </tbody>
</table>

<input data-bind="value: customerName, valueUpdate: 'afterkeydown'" placeHolder="Customer Name"/>
<select data-bind="options: availableMeals, optionsText: 'mealName', value: selectedMeal, optionsCaption: 'Select a meal...'"></select>
<button data-bind="click: addSeat, enable: seats().length < 5 && selectedMeal() && customerName() && customerName().length > 0">Reserve another seat</button>

<强> JS

// Class to represent a row in the seat reservations grid
function SeatReservation(name, initialMeal) {
    var self = this;
    self.name = name;
    self.meal = ko.observable(initialMeal);

    self.formattedPrice = ko.computed(function() {
        var price = self.meal().price;
        return price ? "$" + price.toFixed(2) : "None";        
    });    
}

// Overall viewmodel for this screen, along with initial state
function ReservationsViewModel() {
    var self = this;

    // Non-editable catalog data - would come from the server
    self.availableMeals = [
        { mealName: "Standard (sandwich)", price: 0 },
        { mealName: "Premium (lobster)", price: 34.95 },
        { mealName: "Ultimate (whole zebra)", price: 290 }
    ];

    self.selectedMeal = ko.observable();
    self.customerName = ko.observable();

    // Editable data
    self.seats = ko.observableArray([]);

    // Operations
    self.addSeat = function() {
        self.seats.push(new SeatReservation(self.customerName(), self.selectedMeal()));

        // reset inputs
        self.customerName(undefined);
        self.selectedMeal(undefined);
    }
    self.removeSeat = function(seat) { self.seats.remove(seat) }
}

ko.applyBindings(new ReservationsViewModel());