使用Knockout.JS进行可观察绑定

时间:2014-06-30 13:53:11

标签: javascript html knockout.js

当我运行此html页面并单击select元素中的某个选项时,价格值会自动更改。怎么可能?你能解释一下这个函数以及select elementvalue: meal,的角色价值吗?

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Working with Lists and Collections</title>

    <script type="text/javascript" src="knockout-3.1.0.debug.js"></script>

    <script type="text/javascript">
        function SeatReservation(name, initialMeal) {
            var self = this;
            self.name = name;
            self.meal = ko.observable(initialMeal);
        }

        // 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 }
            ];

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

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

</head>
<body>
    <div>
        <h2>Your seat reservations</h2>
        <table>
            <thead>
                <tr>
                    <th>Passenger name</th>
                    <th>Meal</th>
                    <th>Surcharge</th>
                    <th></th>
                </tr>
            </thead>
            <tbody data-bind="foreach: seats">
                <tr>
                    <td>
                        <input data-bind="value: name" /></td>
                    <td>
                        <select data-bind="options: $root.availableMeals, value: meal, optionsText: 'mealName'"></select></td>
                    <td data-bind="text: meal().price"></td>
                </tr>
            </tbody>
        </table>
        <button data-bind="click: addSeat">Reserve another seat</button>
    </div>
    <script type="text/javascript">
        ko.applyBindings(new ReservationsViewModel());
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

这就是淘汰赛的设计方式。我建议通过Knockout Tutorials来更好地理解它。

但是在回答您的问题时,您的select设置为显示:$root.availableMeals,这是所有餐点的数组,定义为:

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

数组中的每个项目都有2个属性:mealName,这是selectprice中的显示值,用于Surcharge列。显示值设置为:optionsText: 'mealName'

您的用户界面允许您从此列表中选择一个选项,并使用以下内容保存选择:value: meal,定义为每个座位预留的可观察值,使用默认值定义:

self.meal = ko.observable(initialMeal);

一旦做出选择并且此值已更新,则淘汰将开始处理Surcharge列上显示的值。

由于用餐选择已更新并且observable,因此价格会自动更改为与用餐选择一致,因为您的绑定已配置为返回price为座位预订选择的餐点。

<td data-bind="text: meal().price"></td>

JSFiddle For Reference