简体   繁体   English

给定新值时,可观察到的淘汰赛未更新

[英]Knockout Observable not updating when given a new value

This seems like the most basic part of using knockout, and I'm not sure why it isn't working, but for some reason , 2 of my 5 knockout observables are holding onto their new values. 这似乎是使用基因剔除的最基本的部分,但我不确定为什么它不起作用,但是由于某种原因,我的5个基因剔除可观察值中有2个都保持了新的值。

In the setup of the model: 在模型的设置中:

self.CProviderIdentifier = ko.observable();
self.ReferringProviderIdentifier = ko.observable();
self.BillableCareProviderIdentifier = ko.observable();
self.ServiceLocationIdentifier = ko.observable();
self.PracticeLocationIdentifier = ko.observable();

Inside of an AJAX call which returns a number of items inside of a JSON object, I extract the relevant pieces of information, and put them into the correct observable: 在一个AJAX调用中,该调用返回JSON对象内部的许多项目,我提取了相关的信息,并将其放入正确的可观察对象中:

visitModel.CProviderIdentifier(data.CareProviderIdentifier);
visitModel.ReferringProviderIdentifier((data.ReferringProviderIdentifier == null ||
  data.ReferringProviderIdentifier == "undefined") ? 0 : data.ReferringProviderIdentifier);
visitModel.BillableCareProviderIdentifier(data.BillableCareProviderIdentifier);
visitModel.PracticeLocationIdentifier(data.PracticeLocationIdentifier);
visitModel.ServiceLocationIdentifier(data.ServiceLocationIdentifier);

Now, if none of them worked, it would make (some) sense, but only CProviderIdentifier and ReferringProviderIdentifier have no data. 现在,如果它们都不起作用,那么(某种意义上)就可以了,但是只有CProviderIdentifier和ReferringProviderIdentifier没有数据。 I've checked the data in a break point right before getting into setting the properties, and the values from data are 1003 and 0, but the two observables are undefined are the above block of code. 在设置属性之前,我已经在断点处检查了数据,数据中的值为1003和0,但是上面的代码块未定义两个可观察值。

I'm working on getting a fiddle working for this: https://jsfiddle.net/bz3mq6z9/ 我正在为此工作: https//jsfiddle.net/bz3mq6z9/

The assignment is made in the loadData function. 分配是在loadData函数中进行的。 Inside it, Javascript does not know what is visitModel. 在其中,Javascript不知道什么是visitModel。 That variable does not exist and does not have any purpose in the setter. 该变量不存在,在setter中没有任何用途。

Use self instead of visitModel. 使用self而不是visitModel。 That way knockout knows that he is assigning values to the view model 这样敲除就知道他正在为视图模型分配值

Greetings 问候

You have some bugs in your code: 您的代码中有一些错误:

  1. should bind viewmode with DOM using: 应该使用以下方式将viewmode与DOM绑定:

    ko.applyBindings(visitModel);

  2. ko.observable is a function, so you should use call it before combine string. ko.observable是一个函数,因此您应该在合并字符串之前使用调用它。

    <span data-bind="text: CProviderIdentifier() + 'cp'"></span>

  3. it's not a good idea use visitModel in LoadData function, you can just use self to keep the reference. LoadData函数中使用visitModel不是一个好主意,您可以仅使用self保留引用。

see this demo: http://jsfiddle.net/bz3mq6z9/6/ 观看此演示: http : //jsfiddle.net/bz3mq6z9/6/

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

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