2012-12-24 60 views
1

內使用時如不方便綁定都不能正常工作我使用的是淘汰賽JSknockoutjs如果和模板

<tbody data-bind="foreach: List"> 
      <tr> 
       <td><input type="button" data-bind="if: ($parent.IsFVL || $parent.AllowChat || $root.Me().AllowChatMonitoring), value: ID, click: SelectVisit" /><span data-bind="ifnot: ($parent.IsFVL || $parent.AllowChat || $root.Me().AllowChatMonitoring), text: ID"></span></td>... 

下面的模板並調用/啓動模板這樣

<div data-bind="template: { name: 'tplVisitsGrid', data: { Title: 'My Visits', 'List': MVL, 'AllowChat': true, 'AllowPing': false, 'IsFVL': false } }"></div> 

也我已經仔細檢查了「$ root.Me()。AllowChatMonitoring」的值是否爲true,但輸入[type = button]和span都呈現,有人可以幫我解決問題。

謝謝。

回答

0

因爲您將多個屬性合併到ififnot中,您需要對它們進行評估。試試這個(假設IsFVLAllowChatAllowChatMonitoring都觀察到的 - 它們必須):

<td><input type="button" data-bind="if: ($parent.IsFVL() || $parent.AllowChat() || $root.Me().AllowChatMonitoring()), value: ID, click: SelectVisit" /><span data-bind="ifnot: ($parent.IsFVL() || $parent.AllowChat() || $root.Me().AllowChatMonitoring()), text: ID"></span></td>... 

什麼是實際發生之前是,它是比較3層的功能,不是的函數的返回值。

+0

這些屬性是不可觀測的,所以你的建議將無法正常工作。 –

+0

@ArtemVyshniakov對於他們在'If'綁定工作,他們*必須*可觀察 – xdumaine

0

訪問IsFVL,AllowChat等時,不應該使用$parent嘗試到您的模板更新到如下:

<input type="button" data-bind="if: (IsFVL || AllowChat || $root.Me().AllowChatMonitoring), value: ID, click: SelectVisit" /> 
<span data-bind="ifnot: (IsFVL || AllowChat || $root.Me().AllowChatMonitoring), text: ID"></span></td> 

如果這不會幫助,創建您的問題小提琴。

+0

感謝您的回覆,屬性IsFVL和AllowChat不是可觀察的,不讓他們觀察的原因是我加載模板三次使用這些屬性的不同值,所以我找不到一種方法將它們添加爲可觀察到的模型。通過檢查其他簡單的JavaScript表達式在敲除綁定中進行評估,如if,ifnot,visible,但這些props不是。我將爲此和帖子創建一個小提琴。 – Liono