当前位置:网站首页>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边栏推荐
- Micro service gateway selection, please accept my knees!
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- 原生js添加样式的方法
- Share how to make professional hand drawn electronic maps
- C语言,实现三子棋小游戏
- kubernetes资源对象介绍及常用命令(四)
- SimpleITK使用——4. 奇怪的问题
- Lightgbm principle and its application in astronomical data
- 建立自己的网站(22)
猜你喜欢
sql service 截取字符串
Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
Hanoi Tower problem
"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!
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
任务和特权级保护
建立自己的网站(22)
[shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
大话云原生之负载均衡篇-小饭馆客流量变大了
随机推荐
NC50965 Largest Rectangle in a Histogram
Lightgbm principle and its application in astronomical data
Oracle cursor
U++ 原始内存 学习笔记
数据库系统概论第一章简答题-期末考得怎么样?
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
Task and privilege level protection
#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)
[shutter] shutter resource file use (import resource pictures | use image resources)
UE4 game architecture learning notes
Technological Entrepreneurship: failure is not success, but reflection is
Oracle-PL/SQL编程
NC50965 Largest Rectangle in a Histogram
Attack and defense world PWN question: Echo
"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!
Mathematical modeling -- graph and network models and methods (I)
JS solution for obtaining the width and height of hidden elements whose display is none
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)