KnockoutJS教程

KnockoutJS 依赖跟踪

当值更新时,KnockoutJs 会自动跟踪依赖项。它有一个名为 dependency tracker (ko.dependencyDetection) 的对象,它充当订阅依赖项的两方之间的中介。
以下是依赖跟踪的算法。
第 1 步-每当您声明一个计算的 observable 时,KO 会立即调用其评估器函数以获取其初始值。
第 2 步-订阅设置为评估者读取的任何 observable。在应用程序中,不再使用的旧订阅被丢弃。
第 3 步-KO 最终通知更新的计算 observable。
示例
<!DOCTYPE html>
<html>
   <head>
      <title>KnockoutJS How Dependency Tracking Works</title>
      <!--CDN's-->
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js"
         type = "text/javascript"></script>
   </head>
   
   <body>
      <div>
         <form data-bind = "submit: addFruits">
            <b>Add Fruits:</b>
            <input data-bind = 'value: fruitToAdd, valueUpdate: "afterkeydown"'/>
            <button type = "submit" data-bind = "enable: fruitToAdd().length > 0">Add</button>
            <p><b>Your fruits list:</b></p>
            <select multiple = "multiple" width = "50" data-bind = "options: fruits"> </select>
         </form>
      </div>
      
      <script>
         var Addfruit = function(fruits) {
            this.fruits = ko.observableArray(fruits);
            this.fruitToAdd = ko.observable("");
            
            this.addFruits = function() {
               
               if (this.fruitToAdd() != "") {
                  this.fruits.push(this.fruitToAdd());   // Adds a fruit
                  this.fruitToAdd("");                   // Clears the text box
               }
                
            }.bind(this);                                // "this" is the view model
         };
         ko.applyBindings(new Addfruit(["Apple", "Orange", "Banana"]));
      </script>
      
   </body>
</html>
输出
让我们执行以下步骤来看看上面的代码是如何工作的-
将上述代码保存在 dependency_tracking.htm 文件中。 在浏览器中打开此 HTML 文件。 输入任何水果名称并点击添加按钮。

使用 Peek 控制依赖

通过使用 peek 函数,可以在不创建依赖项的情况下访问计算的 Observable。它通过更新计算属性来控制 Observable。
示例
<!DOCTYPE html>
<html>
   <head>
      <title>KnockoutJs Controlling Dependencies Using Peek</title>
      <!--CDN's-->
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js"
         type = "text/javascript"></script>
   </head>
   
   <body>
      <div class = "logblock">
         <h3>Computed Log</h3>
         <pre class = "log" data-bind = "html: computedLog"></pre>
      </div>
      <script>
         function AppData() {
            this.firstName = ko.observable('John');
            this.lastName = ko.observable('Burns');
            this.computedLog = ko.observable('Log: ');
            
            this.fullName = ko.computed(function () {
               var value = this.firstName() + " " + this.lastName();
               this.computedLog(this.computedLog.peek() + value + '; <br/>');
               return value;
            }, this);
            this.step = ko.observable(0);
            this.next = function () {
               this.step(this.step() === 2 ? 0 : this.step()+1);
            };
         };
         
         ko.applyBindings(new AppData());
      </script>
      
   </body>
</html>
输出
让我们执行以下步骤来看看上面的代码是如何工作的-
将上述代码保存在 dependency_tracking_peek.htm 文件中。 在浏览器中打开此 HTML 文件。

观察

忽略计算依赖项中的依赖项

ko.ignoreDependencies 函数有助于忽略那些您不想在计算依赖项中跟踪的依赖项。以下是它的语法。
ko.ignoreDependencies( callback, callbackTarget, callbackArgs );

为什么循环依赖没有意义

如果 KO 正在评估一个 Computed Observable,那么它不会重新开始对依赖的 Computed Observable 的评估。因此,在您的依赖链中包含循环是没有意义的。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4