gpt4 book ai didi

javascript - Knockout.js 可观察数组操作

转载 作者:行者123 更新时间:2023-12-03 04:54:29 24 4
gpt4 key购买 nike

我对knockout.js有点陌生,我很难理解在可观察数组上放置某些操作的位置。我的代码如下所示:

var Building = function() {
var self = this;
self.nLocation = ko.observable();
self.nBuilding = ko.observable();
...
self.increaseLocNum = function() {
self.nLocation(self.nLocation + 1);
};
}
var Quote = function() {
var self = this;
var nQuoteID = ko.observable();
...
self.buildings = ko.observableArray([]);
self.addBuilding = function(){
...
// Build new building with next loc/building number
buildings.push();
}
...
}
ko.applyBindings(new Quote());

所以本质上我有一个报价,上面可以有多个建筑物。每个建筑物都绑定(bind)到选项卡控件上的不同选项卡。这些选项卡上有一个“位置”字段,其中包含可增加/减少位置编号的 +/- 按钮。

我需要使用“启用”绑定(bind)来设置 +/- 按钮的启用(例如,如果建筑物是位置编号最高的唯一建筑物。以下是一些简单规则的示例:

  • 如果建筑物位于位置 1,则位置编号无法减少
  • 如果该建筑物是最高位置上的唯一建筑物,则无法增加位置编号
  • 如果只有一栋建筑物,则无法增加位置编号

逻辑非常简单,但我不知道这个逻辑应该遵循最佳 knockout 实践。

最佳答案

减少函数很简单,因为单个构建 View 模型不需要知道任何外部因素来做出决定;它可以根据其可观测位置进行简单计算。增加函数比较棘手,因为它取决于集合中所有其他建筑物的状态。

这里有一个选项可以避免子类必须了解父类。在每个建筑物上放置一个 canIncrease observable,但让父 View 模型处理实际的增加/减少,以便它可以循环所有 subview 并在位置更改时更新其 observable。

var Building = function(location) {
var self = this;
self.nLocation = ko.observable(location);
self.nBuilding = ko.observable("-Building Name Here-");

self.canIncrease = ko.observable(); //set by parent
self.canDecrease = ko.computed(function(){
//Location number can't be decreased if the building is at location 1
return self.nLocation() > 1;
});
}

var Quote = function() {
var self = this;
var nQuoteID = ko.observable();

self.buildings = ko.observableArray([new Building(1)]);

self.addBuilding = function() {
// Build new building with next loc/building number
self.buildings.push(new Building(self.highestLocation() + 1));
self.enableChildren();
}

self.highestLocation = ko.computed(function(){
var highest=0;
$.each(self.buildings(), function(key,value){ if(value.nLocation() > highest) highest = value.nLocation(); });
return highest;
});

self.increaseLocNum = function(building) {
building.nLocation(building.nLocation() + 1);
self.enableChildren();
};
self.decreaseLocNum = function(building) {
building.nLocation(building.nLocation() - 1);
self.enableChildren();
};

self.enableChildren = function(){
//Location number can't be increased if the building is the only building on the highest location
//Location number can't be increased if there is only one building
$.each(self.buildings(), function(key, building){
if(building.nLocation() === self.highestLocation() || self.buildings().length === 1){
building.canIncrease(false);
}else{
building.canIncrease(true);
}
});
}
}

ko.applyBindings(new Quote());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<input type="button" data-bind="click: addBuilding" value="Add Building" />
<span style="margin-left: 8px;">Total Buildings: <label data-bind="text: buildings().length" /></span>
<span style="margin-left: 8px;">Highest Location: <label data-bind="text: highestLocation" /></span>
<br/>
<table>
<thead>
<tr>
<th></th>
<th>Location</th>
<th>Building Name</th>
</tr>
</thead>
<tbody data-bind="foreach: buildings">
<tr style="border: 1px solid blue;">
<td>
<input type="button" data-bind="enable: canDecrease, click: $parent.decreaseLocNum" value="-" />
<input type="button" data-bind="enable: canIncrease, click: $parent.increaseLocNum" value="+" />
</td>
<td>
<span data-bind="text: nLocation"></span>
</td>
<td>
<span data-bind="text: nBuilding"></span>
</td>
</tr>
</tbody>
</table>

关于javascript - Knockout.js 可观察数组操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42493997/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com