当前位置:网站首页>How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
2022-07-02 22:44:00 【Bustling city 】
problem :
Inside a knockout.js binding expression, I can use the $data , $parent , and $root pseudovariables . stay knockout.js Binding expression , I can use $data , $parent and $root Pseudo variable .How can I get the equivalent of those pseudovariables when I'm using a ko.computed observable declared in JavaScript? When I use in JavaScript Declarative ko.computed observable when , How to obtain the equivalent of these pseudo variables ?
I've got a parent viewmodel with a collection of children, and the parent viewmodel has a selectedChild observable. I have a parent view model with subsets , The parent view model has a selectedChild observable.Given that, I can use databinding expressions to add a CSS class to whichever child is currently selected: In view of this , I can use data binding expressions to CSS Class is added to the currently selected child :
<ul data-bind="foreach: children"> <li data-bind="text: name, css: {selected: $data === $root.selectedChild()}, click: $root.selectChild"></li></ul><script>vm = { selectedChild: ko.observable(), children: [{name: 'Bob'}, {name: 'Ned'}], selectChild: function(child) { vm.selectedChild(child); }};ko.applyBindings(vm);</script>But my viewmodels are going to get more complex, and I'd like "am I selected?" But my view model will become more complex , I think “ Have I been chosen ?”to be able to do more than just adding a single CSS class to a single element. What we can do is not just to put a single CSS Class is added to a single element .I really want to make an isSelected computed property on the child viewmodel, so I can then add other computed properties that depend on it. I really want to create one on the subview model isSelected Compute properties , So I can add other computational properties that depend on it .
I've tried just writing JavaScript that refers to $data and $root , on the off-chance that knockout might define those variables and somehow have them be in scope when it calls my computed evaluator function: I try to write references $data and $root JavaScript, Because elimination may define these variables , And when it calls my computed When assigning a function , They will put them in range in some way :
{ name: 'Bob', isSelected: ko.computed(function(){ return $data === $root.selectedChild(); })}But no such luck: inside my evaluator function , both $data and $root are undefined . But there is no such luck : In my assessment function , $data and $root All are undefined .
I've also tried using ko.contextFor inside my evaluator, since it does give access to $data and $root . I also try to use it in my evaluator ko.contextFor , Because it can really access $data and $root .Unfortunately, inside my evaluator function, contextFor also always returns undefined . Unfortunately , In my evaluation function , contextFor Always return undefined .(I didn't hold out high hopes for this strategy anyway -- it's not clear how well knockout would be able to track the dependencies if I had to go behind its back like this.)( in any case , I have no high hopes for this strategy - If I have to chase it like this , So it's not clear how the knockout can track dependencies .)
I could always manually set a property on each child viewmodel that refers back to the parent viewmodel. I can always manually set a property on each subview model , This view returns to the parent view model .But I know that knockout has the ability to do this for me, and I'd like to at least explore whether I can use its mechanisms before I go writing my own. But I know the knockout is capable of doing this for me , I want to explore whether I can use its mechanism at least before I write it myself .
It seems like it should be possible to translate the above binding expression to a computed observable -- after all, that's what knockout already does : It seems that it should be possible to convert the above binding expression to computed observable - After all , This is what elimination has done :
The other neat trick is that declarative bindings are simply implemented as computed observables. Another clever trick is that declarative binding is only for computation observable Realization .
But how do I go about dealing with the $data and $root pseudovariables when I'm writing my own computed observable? But when I write my own computational observables , How to deal with it $data and $root Pseudo variable ?
Solution :
Reference resources : https://stackoom.com/en/question/aFqu边栏推荐
- 数学建模——图与网络模型及方法(一)
- [foreign journal] sleep and weight loss
- Source code analysis - lightweight asynchronous crawler framework Ruia
- 服务器响应状态码
- Market Research - current market situation and future development trend of aircraft audio control panel system
- sql service 截取字符串
- U++ 原始内存 学习笔记
- Official announcement! The golden decade of new programmers and developers was officially released
- NC24325 [USACO 2012 Mar S]Flowerpot
- Servicemesh mainly solves three pain points
猜你喜欢

大话云原生之负载均衡篇-小饭馆客流量变大了

NC50965 Largest Rectangle in a Histogram

对象与对象变量

Socket套接字C/S端流程

#include errors detected. Please update your includePath.
![[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)](/img/27/8594ba0b49d5008b7469967babed17.jpg)
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)

Oracle-游标

Task and privilege level protection

Share how to make professional hand drawn electronic maps

"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
随机推荐
Notes on key vocabulary of the original English book biography of jobs (IX) [chapter seven]
Commodity information management system (C language document version)
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
20220702 how do programmers build knowledge systems?
Oracle PL / SQL programming
Struct, bit segment, enumeration, union
Bridge emqx cloud data to AWS IOT through the public network
Market Research - current situation and future development trend of marine clutch Market
【leetcode】1380. Lucky number in matrix
UE4 游戏架构 学习笔记
Regular expression (2)
C language, to achieve three chess games
Market Research - current market situation and future development trend of genome editing mutation detection kit
[001] [arm-cortex-m3/4] internal register
Technological Entrepreneurship: failure is not success, but reflection is
Market Research - current market situation and future development trend of handheld wound imaging equipment
Objects and object variables
What is it that makes you tremble? Those without fans can learn
php实现根据输入的年龄查询出生日期符合的数据
大话云原生之负载均衡篇-小饭馆客流量变大了