0

在下面的班级

thisin指的new SeatReservation("Steve", this.availableMeals[0]),是 inside seats,我如何让它指正确的availableMeals

我可以去吗new SeatReservation("Steve", parent.this.availableMeals[0]),

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

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

    // Editable data
    this.seats = ko.observableArray([
        new SeatReservation("Steve", this.availableMeals[0]),
        new SeatReservation("Bert", this.availableMeals[0])
    ]);

    // Operations
    this.addSeat = function() {
        self.seats.push(new SeatReservation("", this.availableMeals[0]));
    }
}
4

2 回答 2

5

使用self您注释掉的变量

self.availableMeals[0]
于 2013-04-19T14:05:09.580 回答
1

它也应该适用this,因为您没有切换上下文。可能 HTML 中的绑定有问题。我创建了一个JS Fiddle,希望能帮到你。

编辑:啊,如果你的意思是里面的语句addSeat,你真的需要这个self变量,或者你将正确的对象应用为this

this.addSeat = function() {
    this.seats.push(new SeatReservation("", this.availableMeals[0]));
}.apply(this);
于 2013-04-19T14:19:35.167 回答