Javascript 用模型剔除计算柱

Javascript 用模型剔除计算柱,javascript,c#,knockout.js,knockout-mvc,Javascript,C#,Knockout.js,Knockout Mvc,我的MVC解决方案中有一个具有以下属性的模型 在我的javascript中,我可以调用模型并显示数据,但是当涉及到使用某些计算函数时,它就失败了 Javascript var registrationRequirementModel = { frenchData: ko.observable(""), genderData: ko.observable(""), loadIntentData: ko.observable(""),

我的MVC解决方案中有一个具有以下属性的模型

在我的javascript中,我可以调用模型并显示数据,但是当涉及到使用某些计算函数时,它就失败了

Javascript

    var registrationRequirementModel = {
        frenchData:  ko.observable(""),
        genderData:  ko.observable(""),
        loadIntentData:  ko.observable(""),

        isMissingData: ko.computed(function () {
            if (this.frenchData() == "") { return true };
            if (this.genderData() == "") { return true };
            if (this.loadIntentData() == "") { return true };
            return false;
        },this),

    }

   $(document).ready(function () {

        ko.applyBindings(registrationRequirementModel, document.getElementById("RegistrationSurveyContent"));

        $.ajax({
            url: getStudentRegRequirementsUrl,
            type: "GET",
            contentType: jsonContentType,
            dataType: "json",
            success: function (data) {
                if (!account.handleInvalidSessionResponse(data)) {
                    registrationRequirementModel.frenchData(data.Francophone);
                    registrationRequirementModel.genderData(data.Gender);
                    registrationRequirementModel.loadIntentData(data.LoadIntent);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (jqXHR.status != 0)
                    $('#notificationHost').notificationCenter('addNotification', { message: "Unable to retrieve registration requirement.", type: "error" });
            }
        });
    });
Html


frenchData不是一个函数
控制台错误源于KnockoutJS ViewModel的设置方式。本质上,计算函数
是低于正常观测值的missingData
,它有一个新的内部范围上下文
,它不反映
注册需求模型
对象的相同外部范围

要解决这个问题,您应该从使用
对象文本
切换到
构造函数
,这样您就可以将
视图模型范围分配给
自/那个
变量,从而缓解范围问题。然后通过KO Apply绑定实例化新存储的ViewModel,AJAX成功后您现在可以访问这些绑定:

function registrationRequirementModel() {
  var self = this;
  self.frenchData = ko.observable("");
  self.genderData = ko.observable("");
  self.loadIntentData = ko.observable("");

  self.isMissingData = ko.computed(function() {
    if (self.frenchData() == "") {
      return true
    };
    if (self.genderData() == "") {
      return true
    };
    if (self.loadIntentData() == "") {
      return true
    };
    return false;
  }, this);
}

$(document).ready(function() {
  var vm = new registrationRequirementModel();
  ko.applyBindings(vm, document.getElementById("RegistrationSurveyContent"));

  // replace with endpoint
  var jsonData = {
    Francophone: "Francophone",
    Gender: "Male",
    LoadIntent: "LoadIntent"
  };

  if (handleInvalidSessionResponse(jsonData)) {
    vm.frenchData(jsonData.Francophone);
    vm.genderData(jsonData.Gender);
    vm.loadIntentData(jsonData.LoadIntent);
  }
});

function handleInvalidSessionResponse(data) {
  if (typeof data !== "undefined") return true;
  return false;
}
下面是该场景的模拟部分

定义viewmodel时,
不指向新创建的对象,它指向创建它的上下文中的任何
(可能是
窗口

var-vm={
计算使用:ko.computed(函数(){
归还这个;
},本页)
}
console.log(
vm.computedUsingThis()==vm,//false
vm.computedUsingThis()==window//true
);

我仍然会遇到错误,我注意到当我从ajax调用frenchData时,错误就发生了。从ajax调用中提供返回的JSON,以便我们可以使用模拟数据。只能通过完整的问题提供帮助。例如,我们不知道
帐户
GetStudentRequirementSurl
应该是什么。@Jseb根据一些假设进行了更新和解决。
<table style="width:100%">
    <tbody>
        <tr>
            <td data-bind="text: loadIntentData"></td>
            <td data-bind="text: frenchData"></td>
            <td data-bind="text: genderData"></td>
        </tr>
    </tbody>
</table>
 public async Task<JsonResult> GetRegistrationRequirementAsync()
 {
     string StudentID = CurrentUser.PersonId;
     try
     {
         var requirement = await ServiceClient.L09GetRegistrationRequirementAsync(StudentID);
         RegistrationRequirementModel registrationRequirementModel = new RegistrationRequirementModel(requirement);
         return Json(registrationRequirementModel, JsonRequestBehavior.AllowGet);
      }
      catch (Exception e)
      {}
}
function registrationRequirementModel() {
  var self = this;
  self.frenchData = ko.observable("");
  self.genderData = ko.observable("");
  self.loadIntentData = ko.observable("");

  self.isMissingData = ko.computed(function() {
    if (self.frenchData() == "") {
      return true
    };
    if (self.genderData() == "") {
      return true
    };
    if (self.loadIntentData() == "") {
      return true
    };
    return false;
  }, this);
}

$(document).ready(function() {
  var vm = new registrationRequirementModel();
  ko.applyBindings(vm, document.getElementById("RegistrationSurveyContent"));

  // replace with endpoint
  var jsonData = {
    Francophone: "Francophone",
    Gender: "Male",
    LoadIntent: "LoadIntent"
  };

  if (handleInvalidSessionResponse(jsonData)) {
    vm.frenchData(jsonData.Francophone);
    vm.genderData(jsonData.Gender);
    vm.loadIntentData(jsonData.LoadIntent);
  }
});

function handleInvalidSessionResponse(data) {
  if (typeof data !== "undefined") return true;
  return false;
}