简体   繁体   English

Knockout计算在页面加载时执行

[英]Knockout computed executed on page load

Basically i have a drop down on selecting which there will be another drop down loaded. 基本上我有一个下拉选择哪个将有另一个下拉加载。 I have a computed variable depending on first drop down selected value(I know it can be subscribed,but still). 我有一个计算变量取决于第一个下拉选择值(我知道它可以订阅,但仍然)。 But the computed is executed on page load which i dont want due to an AJAX call inside. 但是计算是在页面加载上执行的,由于内部的AJAX调用,我不想要。 'What is the reason for the execution on page load and how to avoid that? '在页面加载上执行的原因是什么以及如何避免这种情况?

HTML: HTML:

<div>
  <select id="selectmenu1" data-bind="options: departments, 
             optionsValue: 'id', 
             optionsText: 'name', 
             optionsCaption: 'Choose...',value: selectedDept">
  </select>
  <select id="selectmenu1" data-bind="options: contacts, 
             optionsValue: 'id', 
             optionsText: 'name', 
             optionsCaption: 'Choose...'">
  </select>
</div>

And JS 和JS

// Here's my data model
var ViewModel = function(first, last) {
  var self = this;
  var deptArray = [];
  var deptObj = {
    id: "8888",
    name: "Electrical"
  };
  deptArray[0] = deptObj;
  deptObj = {
    id: "9999",
    name: "Admin"
  };
  deptArray[1] = deptObj;
  self.departments = ko.observableArray(deptArray);
  self.selectedDept = ko.observable();
  self.contacts = ko.observableArray();
  self.contactsRetrieve = ko.computed(function() {
    var deptId = self.selectedDept();
    console.log("entered");
    $.ajax({
      url: '/echo/js',
      complete: function(response) {
        console.log("success");
        var contactArray = [];
        var contactObj = {};
        if (deptId == '8888') {
          contactObj.id = '1234';
          contactObj.name = 'Vivek';
        } else if (deptId == '9999') {
          contactObj.id = '5678';
          contactObj.name = 'Sree';
        }
        contactArray[0] = contactObj;
        self.contacts(contactArray);
      }
    });
    console.log("exited");
  });
};

ko.applyBindings(new ViewModel());

https://jsfiddle.net/jtjozkax/37/ https://jsfiddle.net/jtjozkax/37/

As far as I know, it is not the page load itself that causes this behavior, but the fact that computed observables rely on other observables, in your case, self.selectedDept . 据我所知,导致此行为的不是页面加载本身,而是计算的observable依赖于其他可观察对象的事实,在您的情况下, self.selectedDept Knockout discovers which other observables the computed relies on, and it is actually the change of value of selectedDept what causes the computed to be recomputed. Knockout发现计算所依赖的其他可观察量,实际上是selectedDept的值的变化是什么原因导致重新计算计算结果。

You cannot avoid this behavior, but you can avoid the execution of the AJAX call by adding a guarding condition (read: if statement). 您无法避免此行为,但可以通过添加保护条件(读取: if语句)来避免执行AJAX调用。 I assume your goal is to prevent the AJAX-call if nothing is selected in the dropdown, and, afterall, this is the most straightforward way to accomplish just that. 我假设你的目标是在下拉列表中没有选择任何内容时阻止AJAX调用,并且,毕竟,这是实现这一目标的最直接的方法。

There is no other way around it, simply because if you think about it, the whole purpose of a computed is to reevalue itself whenever any other observable it depends on changes, without manual intervention. 没有别的方法,只是因为如果你考虑它,计算的整个目的是在没有人工干预的情况下,每当任何其他可观察的它依赖于变化时重新评估它自己。

if(!self.selectedDept()){return}

Using this if statement to cancel functionality within the computed if there is no selected option. 如果没有选择选项,则使用此if语句取消计算中的功能。

https://jsfiddle.net/jtjozkax/38/ https://jsfiddle.net/jtjozkax/38/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM