如何在 KnockoutJS 中构建正确的视图模型(具有多个依赖项)?

发布于 2024-12-15 18:18:42 字数 491 浏览 0 评论 0原文

这是我正在使用的标记的子集:

<div id="item_one"></div>
<div id="item_two"></div>
<div id="item_three"></div>

<p id="result_one">0</p>
<p id="result_two">0</p>
<p id="result_three">0</p>

所需的行为是:

  1. 当您单击 div 时,p 标签的相应文本将从 0 切换到 1。p
  2. 标签的文本将连接成字符串,例如,单击第二项,结果字符串将为“010”。
  3. 有一个由八项组成的数组,以二进制字符串作为键。单击时,数组中的选定项目会发生变化。

这似乎是淘汰赛的一个很好的用途,但我是一个十足的菜鸟。如何设置正确的依赖关系?

Here is a subset of the markup I am using:

<div id="item_one"></div>
<div id="item_two"></div>
<div id="item_three"></div>

<p id="result_one">0</p>
<p id="result_two">0</p>
<p id="result_three">0</p>

The desired behaviors are:

  1. When you click a div, the corresponding text of the p tag is toggled from 0 to 1.
  2. The text of the p tags is to be concatenated into a string, e.g., click the second item and the resulting string would be "010".
  3. There is an array of eight items, with binary strings as the key. As clicks are made, the selected item in the array changes.

This seems like a good use of knockout, but I am a complete noob. How do I set up the proper dependencies?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年不长 2024-12-22 18:18:42

以下是一种方法的示例: http://jsfiddle.net/rniemeyer/XqDsy/

为了方便起见,我创建了一个小的“binaryObservable”来公开切换函数。

function binaryObservable(initialValue) {
   var result = ko.observable(initialValue);
   result.toggle = function() {
       result(result() === "1" ? "0" : "1");    
   };
   return result;
}

function ViewModel() {
   this.one = binaryObservable("0");
   this.two = binaryObservable("0");
   this.three = binaryObservable("0"); 

   this.combined = ko.dependentObservable(function() {
      return this.one() + this.two() + this.three();       
   }, this);

   this.choices = {
        "000": { id: 0, value: "000" },
        "001": { id: 1, value: "001" },
        "010": { id: 2, value: "010" },
        "011": { id: 3, value: "011" },
        "100": { id: 4, value: "100" },
        "101": { id: 5, value: "101" },
        "110": { id: 6, value: "110" },
        "111": { id: 7, value: "111" }
   };

   this.selectedChoice = ko.dependentObservable(function() {
       var combined = this.combined();
       return combined ? this.choices[combined] : {};
   }, this);   
}

ko.applyBindings(new ViewModel());

那么 HTML 可能如下所示:

<div id="item_one" data-bind="click: one.toggle">option one</div>
<div id="item_two" data-bind="click: two.toggle">option two</div>
<div id="item_three" data-bind="click: three.toggle">option three</div>

<hr/>

<p id="result_one" data-bind="text: one">0</p>
<p id="result_two" data-bind="text: two">0</p>
<p id="result_three" data-bind="text: three">0</p>

<hr/>

<p data-bind="text: combined"></p>

<hr/>

Selected choice: <span data-bind="text: selectedChoice().id"></span>

Here is a sample for one way to do it: http://jsfiddle.net/rniemeyer/XqDsy/

For convenience, I created a little "binaryObservable" that exposes a toggle function.

function binaryObservable(initialValue) {
   var result = ko.observable(initialValue);
   result.toggle = function() {
       result(result() === "1" ? "0" : "1");    
   };
   return result;
}

function ViewModel() {
   this.one = binaryObservable("0");
   this.two = binaryObservable("0");
   this.three = binaryObservable("0"); 

   this.combined = ko.dependentObservable(function() {
      return this.one() + this.two() + this.three();       
   }, this);

   this.choices = {
        "000": { id: 0, value: "000" },
        "001": { id: 1, value: "001" },
        "010": { id: 2, value: "010" },
        "011": { id: 3, value: "011" },
        "100": { id: 4, value: "100" },
        "101": { id: 5, value: "101" },
        "110": { id: 6, value: "110" },
        "111": { id: 7, value: "111" }
   };

   this.selectedChoice = ko.dependentObservable(function() {
       var combined = this.combined();
       return combined ? this.choices[combined] : {};
   }, this);   
}

ko.applyBindings(new ViewModel());

Then the HTML might look like:

<div id="item_one" data-bind="click: one.toggle">option one</div>
<div id="item_two" data-bind="click: two.toggle">option two</div>
<div id="item_three" data-bind="click: three.toggle">option three</div>

<hr/>

<p id="result_one" data-bind="text: one">0</p>
<p id="result_two" data-bind="text: two">0</p>
<p id="result_three" data-bind="text: three">0</p>

<hr/>

<p data-bind="text: combined"></p>

<hr/>

Selected choice: <span data-bind="text: selectedChoice().id"></span>
掩于岁月 2024-12-22 18:18:42

我距离专家还很远,但是这样的事情吗?
jsFiddle

Im far from an expert but something like this ?
jsFiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文