英文:
How does data bind paranthesis work in Knockout.js
问题
当我不使用括号时,会发生这种情况,其中的逻辑是什么?
第一个情况:isViewMode = true
,isNotEditableInvoice = false
=> readonly 被评估为 true
第二个情况:isViewMode = false
,isNotEditableInvoice = true
=> readonly 被评估为 false
第三个情况:isReadOnly = false
,isEditableInvoice = false
=> visible 被设置为 false
第四个情况:当我为 isReadOnly
使用括号时,它正常工作,结果与上面相同,但不知何故被评估为 true。为什么 isEditableInvoice
在没有括号的情况下被排除在评估之外?
为什么在不使用括号时会发生这种情况,背后的逻辑是什么?
英文:
I have this input and I want to change its status, so it can be readonly or editable.
<input type="text" data-bind="attr: {readonly: isViewMode || isNotEditableInvoice}, value: model.DateFormatted" />
The problem is when I don't use paranthesis for the variables.
First case: isViewMode = true
, isNotEditableInvoice = false
=> readonly is evaluated to true
Second case: isViewMode = false
, isNotEditableInvoice = true
=> readonly is evaluated to false
Also I have this button, and I want to show it only when the user is in edit mode.
<input type="button" value="Save" data-bind="visible: !isReadOnly || isEditableInvoice,click: onUpdate" />
I have a similar problem with using paranthesis, and I want to know what is happening there.
Third case: isReadOnly = false
, isEditableInvoice = false
=> visible is set to false
Fourth case:
When I use paranthesis for isReadOnly
it's working fine, the result is the same as above, but somehow it is evaluated to true. Why isEditableInvoice
is excluded from evaluation without paranthesis?
<input type="button" value="Save" class="btn btn-primary" data-bind="visible: !isReadOnly() || isEditableInvoice,click: onUpdate" />
Why is this happening when I don't use paranthesis and what is the logic behind?
答案1
得分: 4
因为 knockout 解析器/绑定器正在检查您是否正在绑定到一个表达式(而不是一个单一属性),因此它将按原样评估表达式。
除非您明确调用评估,也就是在末尾添加括号,否则您只是评估表达式'!isReadOnly || isEditableInvoice',字段不会被视为可观察字段,而是按原样处理。
isReadOnly 根据 JavaScript 术语评估为非 undefined,因此为 true。
例如,针对单一属性进行的不同绑定,类似于 if/visible 绑定。
您可以这样做:
data-bind="if: model.someObservableProp"
但您不能这样做:
data-bind="if: !model.someObservableProp"
除非像这样添加括号:
data-bind="if: !model.someObservableProp()"
因为一旦添加了!运算符,解析器将其视为 JavaScript 表达式,而不是可观察字段。
英文:
Because the knockout parser/binder is checking that you are binding against an expression ( instead lets say of a single property ) so it will evaluate the expression as-is.
Unless you explicitly call the evaluation a.k.a add the parenthesis at the end, you are just evaluating the expression '!isReadOnly || isEditableInvoice' and the fields are not treated as observables, rather as-is.
isReadOnly is evaluated in js terms as non-undefined, hence true.
For example a different bind, made against a single property would be like the if/visible binds.
You can do
data-bind="if: model.someObservableProp"
but you can not do
data-bind="if: !model.someObservableProp"
without adding the parenthesis like so:
data-bind="if: !model.someObservableProp()"
Because as soon as you add the ! operator, the parsers treats this as a js expression, not as an observable field
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论