对于Knockout.js3.4.2发布,JavaScript的UI库感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解jsuuid库,并且为您提供关于asp.net–Knockout.js–
对于Knockout.js 3.4.2 发布,JavaScript 的 UI 库感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解js uuid库,并且为您提供关于asp.net – Knockout.js – 数据绑定的javascript函数、javascript – Knockout Computed Observable不更新UI、javascript – knockout js单选按钮单击事件重置选择、javascript – Knockout JS和大型模型的宝贵知识。
本文目录一览:- Knockout.js 3.4.2 发布,JavaScript 的 UI 库(js uuid库)
- asp.net – Knockout.js – 数据绑定的javascript函数
- javascript – Knockout Computed Observable不更新UI
- javascript – knockout js单选按钮单击事件重置选择
- javascript – Knockout JS和大型模型
Knockout.js 3.4.2 发布,JavaScript 的 UI 库(js uuid库)
Knockout.js 3.4.2 发布了。Knockout 是个 JavaScript library,帮助创建丰富的显示和编辑器 UI,通过干净的底层数据模型。你可以在任何时候动态更新 U I的所选择部分。
更新内容:
从循环计算中的脏事件停止无限循环 (#1943)
仅更新计算的 if 依赖项实际上已更改; 只是脏数据时不更改(#2174)
仅当发生更改时通知已有订阅; 不通知未订阅者 (#2163)
如果计算之前读取了不同的中间值,则通知依赖性计算返回的 observable 改变 (#1835)
更新纯计算如果依赖项已更改,并且通知正在等待(#2197)
下载地址:
knockout-3.4.2.debug.js
knockout-3.4.2.js
Source code (zip)
Source code (tar.gz)
asp.net – Knockout.js – 数据绑定的javascript函数
<span id="lblSomePropVal" data-bind="text: MySomeFunction(Someproperty())" ></span>
我想要做的是使用我的viewmodel的SomeProperty的值调用MySomeFunction。 My SomeFunction将根据传递的值返回一些文本,并将显示在span lblSomePropVal中。
我尝试了这个例子中写的方式,但是它会引发绑定错误。
我在这里缺少什么东西,还是有什么其他的办法呢?
这是我得到的错误:
Microsoft JScript runtime error: Unable to parse bindings. Message: [object Error]; Bindings value: text: MySomeFunction(Someproperty())
解决方法
var viewmodel = ko.mapping.fromJS(data.d) viewmodel.MySomeFunction = function(...){...};
javascript – Knockout Computed Observable不更新UI
我已经在复选框上实现了父子依赖关系以下规则控制着交互.
>选中“父复选框”后,将选中所有子复选框.
>取消选中“父复选框”后,将取消选中所有子复选框.
>如果未选中任何一个子复选框,则应取消选中父复选框.
>选中所有子复选框后,应选中父复选框.
现在我有一个场景,其中父子依赖行为是动态的,即上述规则仅在用户单击按钮时才开始工作.
我有一个实现,其中父复选框绑定到一个可观察的属性,然后该属性被重写为新的Computed Observable.
更改为Computed Observable后,前2个规则(规则1和2)工作,但最后2个规则(规则3和4)停止工作.我可以在调试器中看到正确返回了返回值,但它没有更新UI.
为了证明我的问题,我创造了2个JS小提琴
> https://jsfiddle.net/agalo/rjc1Lsbe/4/处的小提琴描述了父子关系规则正常工作的情况.在这种情况下,Computed Observable属性最初在对象上定义,不会动态更改.这非常有效.
> https://jsfiddle.net/agalo/uc7yt9tw/4/处的小提琴描述了我目前的实施规则,其中规则3和规则4不起作用.在这种情况下,单击按钮时,将使用计算的Observable覆盖Observable属性.因此,最初不会强制执行父子关系,但是在单击按钮时会强制执行关系.
我不是在寻找替代解决方案,但我有兴趣了解为什么第二小提琴显示行为.
function viewmodel(){ var self = this; var childrenArray = [ {isSelected : ko.observable(true)},{isSelected : ko.observable(true)},{isSelected : ko.observable(true)}]; self.parentChildData = { parentCheckBox: ko.observable(false),children: ko.observableArray([]) }; self.parentChildData.children(childrenArray); self.makeDependent = function(){ self.parentChildData.parentCheckBox = ko.computed({ read: function () { var anyChildUnChecked = ko.utils.arrayFirst(self.parentChildData.children(),function (childCheckBox) { return !childCheckBox.isSelected(); }); var result = anyChildUnChecked == null; return result; //return false; },write: function (value) { ko.utils.arrayForEach(self.parentChildData.children(),function (childCheckBox) { childCheckBox.isSelected(value); }); } })}; return self; } ko.applyBindings(new viewmodel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script> <p> Describes my current implementation for which Rule 3 and 4 are not working. In this case,the Observable property is overridden with the computed Observable on the click of the button. So,initially the parent child relationship is not enforced,but on click of button the relationship is enforced. </p> <p> After clicking the button,the first 2 Rules (Rule 1 and 2) works but last 2 Rule (Rule 3 and 4) stops working. I can see in the debugger that the return value is correctly returned but it doesn't Update the UI. </p> <div id="parent"> parent <input type="checkBox" data-bind="checked:parentChildData.parentCheckBox"/> <br/> <div id="child"> Child <!-- ko foreach: parentChildData.children --> <input type="checkBox" data-bind="checked:isSelected"/> <!-- /ko --> </div> <br/> <input type="button" value="Make dependent" data-bind="click:makeDependent"> </div>
解决方法
> parentCheckBox首先被定义为self.parentChildData上的一个observable属性.
> self.parentChildData.children数组使用子项初始化.
> parentCheckBox现在更改为计算的observable.
我在这里看到的问题是,在parentCheckBox被要求对self.parentChildData.children中对子节点所做的更改作出反应之前,这个children数组已经初始化,因此没有创建依赖关系,这就是为什么你没有得到这种行为你期待.
因此,我更改了代码的流程并在parentCheckBox被赋予了computedobservable角色之后初始化了self.parentChildData.children(虽然它不会改变最初定义的方式).
self.parentChildData.parentCheckBox = ko.computed({ read: function() { var anyChildUnChecked = ko.utils.arrayFirst(self.parentChildData.children(),function(childCheckBox) { return !childCheckBox.isSelected(); }); var result = anyChildUnChecked == null; return result; //return false; },write: function(value) { ko.utils.arrayForEach(self.parentChildData.children(),function(childCheckBox) { childCheckBox.isSelected(value); }); } }); self.parentChildData.children(childrenArray); //values are pushed here
此外,这消除了对self.makeDependent函数的需要,虽然我也很奇怪,当self.parentChildData.children在创建计算机之前已经具有值时,该函数如何能够使其工作: – )
已更新fiddle.
javascript – knockout js单选按钮单击事件重置选择
请参阅Fiddle Here
查看型号:
var viewmodel = { wantsspam: ko.observable(true),spamFlavor: ko.observable('cherry'),click: function(model){ console.log(model); } };
视图:
<input type="radio" name="flavorGroup" value="cherry" data-bind="checked: spamFlavor,click:click" />
解决方法
click
event documentation:
By default,Knockout will prevent the
click
event from taking any default action.
…
However,if you do want to let the defaultclick
action proceed,justreturn true
from your click handler function.
所以你的单选按钮被重置,因为你的点击处理程序,并修复它只需要返回真的最后:
click: function(){ alert('Hi'); return true; }
演示JSFiddle.
javascript – Knockout JS和大型模型
我想弄清楚使用KnockoutJS的最佳方法,我需要你的建议.
我的视图模型包含一组“文档”,每个文档都有一组“值”.
使用以下模板将每个“文档”呈现为单独的表:
一切都很好,除非视图模型很大.
例如,如果视图模型的大小约为1兆字节(并且包含80个文档,每个文档包含60个值),则在我的计算机上渲染需要两分多钟.
我想知道是否有一种方法可以显着提高性能…或者放弃Knockout并在服务器端构建html并将其推送到浏览器会更快…
渲染“仅”300kb视图模型需要接近30秒.
“文档”由用户定义,因此甚至有> 2兆字节的场景(我不知道他们为什么这样做).
有没有人有JavaScript大视图模型的经验?
最佳答案
根据您当前无法延迟加载(按需加载)数据的要求,您的数量非常有限.
服务器端HTML
生成HTML服务器端将是最快的方法.但即便如此,如果模型很大,也会有延迟.考虑下载一个5MB的HTML文件,这将花费你的浏览器一些时间来解析和渲染如此大的东西.
仍然使用KNOCKOUT JS
如果您仍想使用Knockout JS框架,那么我最好的建议是将每个Document放在IFRAME中.这不是真正推荐的,会给您的服务器带来额外的负担,但如果正确实施可以加快您的用户体验.
www.mysite.com/view/1
客户端浏览器最初将获得包含所有IFRAME的所有HTML,它将异步地为每个IFRAME分派请求.每个单独的Document请求将使用Knockout JS在其自己的帧中异步呈现.
为了改善用户交互,您甚至可以在IFRAME上设置加载事件. load事件可以重新调整IFRAME的大小,因此没有滚动条,或者从IFRAME中提取HTML并用提取的HTML替换IFRAME元素.
今天的关于Knockout.js 3.4.2 发布,JavaScript 的 UI 库和js uuid库的分享已经结束,谢谢您的关注,如果想了解更多关于asp.net – Knockout.js – 数据绑定的javascript函数、javascript – Knockout Computed Observable不更新UI、javascript – knockout js单选按钮单击事件重置选择、javascript – Knockout JS和大型模型的相关知识,请在本站进行查询。
本文标签: