当前位置:网站首页>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边栏推荐
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- 【微服务|Sentinel】重写sentinel的接口BlockExceptionHandler
- Official announcement! The golden decade of new programmers and developers was officially released
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- [QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
- Radis:Linux上安装Redis(步骤)
- Reading experience of just because
- What is it that makes you tremble? Those without fans can learn
- 建立自己的网站(22)
- 存储单位换算
猜你喜欢
UE4 game architecture learning notes
Build your own website (22)
Pointer and string
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
Share how to make professional hand drawn electronic maps
Evolution of messaging and streaming systems under the native tide of open source cloud
对象与对象变量
Socket套接字C/S端流程
Reading experience of just because
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
随机推荐
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
Market Research - current situation and future development trend of sickle cell therapy Market
What is it that makes you tremble? Those without fans can learn
Unity publishes a method of webgl playing sound
Server response status code
【leetcode】1380. Lucky number in matrix
Leetcode theme [array] -169- most elements
LxC terminal login method
C language, to achieve three chess games
JS solution for obtaining the width and height of hidden elements whose display is none
U++ learning note pile
Utilisation de simpletk - 4. Question étrange
Market Research - current market situation and future development trend of high tibial osteotomy plate
[leetcode] sword finger offer 04 Search in two-dimensional array
Market Research - current market situation and future development trend of aircraft wireless intercom system
Simpleitk use - 3 Common operations
U++ learning notes - relaxation
原生js添加样式的方法
Perceptron model and Application
Web侧防御指南