knockout

Durandal / Knockout - global observable

匿名 (未验证) 提交于 2019-12-03 01:06:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Context In my shell.html I have put a language selection into the header section. It looks like this: <select class="form-control select2" data-bind="options:languages, optionsText:'label', optionsValue:'code', value:selectedLanguage"> </select> My shell.js: When I load other views, this always stays in the header and does not change. Task Whenever the user changes the language, I want to be able to register/notice this on any sub-page and then react accordingly - like showing the text in another language. Question I suppose this is more of

KnockoutJS catch errors Binding

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I want to catch an error, correct it and continue executing the program. http://jsfiddle.net/Gthv9/12/ But, I can't do it! If you click on: "re Check On Model1", "re Check On Model3" - it's ok. If you click on: "re Check On Model1", "re Check On Model2", "re Check On Model3" - there's an error. Uncaught Error : Unable to parse bindings . Message : ReferenceError : name3 is not defined ; Bindings value : text : name3 Why? I wrapped the problem code in a try-catch block (viewModel.recheckData2() ), but, the application crashes on

KnockoutJS Forcing A Computed Observable to Re-Compute

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm new to KnockoutJS and think it's a great tool so far :)! However, I've a question regarding computed observables, and it's as follows. On the KnockoutJS page at http://knockoutjs.com/documentation/computedObservables.html , there's a sample code for formatting the price. The code is as follows. HTML <p>Enter bid price: <input data-bind="value: formattedPrice"/></p> And the JS: I've also copied the code and put a jsFiddle here: http://jsfiddle.net/wDvxw/ My issue is that the value does not update itself when you enter the same thing twice

Knockout Validation of two interdependent fields

匿名 (未验证) 提交于 2019-12-03 00:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Consider the following piece of code - var MyObjectModel = function(myObject){ var self = this; self.myNumber1 = ko.observable(myObject.number1).trimmed(); self.myNumber2 = ko.observable(myObject.number2).trimmed(); I would like to extend myNumber1 and myNumber2, to add knockout validations such that it throws an error if both are empty, and stays fine if either has a value. Any idea how I could go about achieving this ? 回答1: You can use an ko.computed to create a function which checks if at least any of both fields is set and onlyIf in the

KnockoutJS binding when source is null/undefined

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Is there a shorter/cleaner way to do the null/undefined testing? Instead of data - bind = "options: SelectedBusinessLine() ? SelectedBusinessLine().Clusters() : [], i would like data - bind = "options: SelectedBusinessLine().Clusters(), give or take the () Or at least a simpler null operator check '??' SelectedBusinessLine ?? [] Or a binding param to auto check for null or silent fail. Any ideas if this is possible? 回答1: This page provides several solutions. The relevant part is this one: Protecting against null objects If you have

Knockout cleanNode() removes jquery event bindings?

匿名 (未验证) 提交于 2019-12-03 00:46:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Knockout's cleanNode() function seems to be removing jQuery's event bindings on elements inside the node. How can I prevent this? There is not much documentation I could find on cleanNode() either. 回答1: Directly from the knockout docs : "Overriding the clean-up of external data When removing an element, Knockout runs logic to clean up any data associated with the element. As part of this logic, Knockout calls jQuery’s cleanData method if jQuery is loaded in your page. In advanced scenarios, you may want to prevent or customize how this data

KnockoutJS-自定义属性绑定

匿名 (未验证) 提交于 2019-12-02 23:56:01
  在knockoutjs中,已有的绑定功能已经十分强大,基本上可以不需要再去考虑扩展了,但是,也有例外的场景,面对这种场景,还是得去完成,knockoutJS提供了自定义绑定来扩展绑定功能。 一、新建绑定    新建一个js文件来尝试绑定功能,按照给定的格式ko.bindingHandlers.xxx来扩展一个绑定。 ko . bindingHandlers . yourBindingName = { init : function ( element , valueAccessor , allBindingsAccessor , viewModel ) { // This will be called when the binding is first applied to an element // Set up any initial state, event handlers, etc. here }, update : function ( element , valueAccessor , allBindingsAccessor , viewModel ) { // This will be called once when the binding is first applied to an element, // and again whenever the

KnockoutJS-快速入门

与世无争的帅哥 提交于 2019-12-02 05:50:18
KnockoutJS-快速入门    虽然在WPF中接触过MVVM模式,可是刚开始在Web中接触到Knockout.JS让我大吃一惊,简化了好多工作量,原来可能需要一大堆的JS脚本完成的工作量,被释放许多。接触KnockoutJS一年多了,在好多个项目中也用到过,虽然每个项目中都有点重复造轮子的滋味,但是不得不说,越来越喜欢用KO完成前端的一些工作,虽然现在有许多流行的MVVM/MVC前端框架,如Angular、Vue、React等等,接触过Vue,简单用过一次,接触不深,不敢谈论,也就带过吧。工作之余 ,开始总结自己在使用的工具、框架上用到的,体会到的感受。 一、简单例子   先来看个最简单ko所带来的效果:      看看代码是如何简洁设计的: 1 <div> 2 <label>公司名称</label> 3 <input type="text" data-bind="value:unitName" /><br /> 4 <label>公司地址</label> 5 <input type="text" data-bind="value:address" /> 6 <p data-bind="text:'您的公司名称为:'+unitName()+',公司地址是:'+address()+' .'"></p> 7 </div> 8 9 @section Scripts{ 10

knockoutjs复杂对象的可观察性

不问归期 提交于 2019-11-28 23:53:52
问题 对于一般数据结构: 1、 对于基本类型的数据的变更的可观察性(observable), 可以使用 ko.observable(xxx) 来声明一个 observable对象,   或将其绑定到视图,   或将其绑定到其它 ko.computed 或者 ko.pureComputed 对象中;   或者使用subscribe单独订阅其变化。 2、 对于数组型号数据结构, ko提供 ko.observableArray(xxx), 将数组做成一个可观察对象, 并开放一些类似数组的 方法,   使用这些方法, 一旦数组发生变化, 则观察者能够被通知到。 pop, push, shift, unshift, reverse, sort, splice All of these functions are equivalent to running the native JavaScript array functions on the underlying array, and then notifying listeners about the change: push( value ) — Adds a new item to the end of array. pop() — Removes the last value from the array and returns

KnockoutJS-自定义属性绑定

纵然是瞬间 提交于 2019-11-28 23:01:03
  在knockoutjs中,已有的绑定功能已经十分强大,基本上可以不需要再去考虑扩展了,但是,也有例外的场景,面对这种场景,还是得去完成,knockoutJS提供了自定义绑定来扩展绑定功能。 一、新建绑定    新建一个js文件来尝试绑定功能,按照给定的格式ko.bindingHandlers.xxx来扩展一个绑定。 ko.bindingHandlers.yourBindingName = { init: function(element, valueAccessor, allBindingsAccessor, viewModel) { // This will be called when the binding is first applied to an element // Set up any initial state, event handlers, etc. here }, update: function(element, valueAccessor, allBindingsAccessor, viewModel) { // This will be called once when the binding is first applied to an element, // and again whenever the associated