【发布时间】:2012-12-05 12:52:46
【问题描述】:
我正在使用我在 SO 上找到的一些代码(这里的第二个答案:knockout.js - deferred databinding for modal?)。
但详细信息并未显示在表单中。
rowClick: function(data){
console.log("in row click");
console.log(data); // produces data as per debug below
// load the transaction when a row is clicked
self.EditingTransaction(data);
console.log(self.EditingTransaction()); // produces data as per debug below (not as a observable though)
},
查看代码如下:
<div data-bind="modal: EditingTransaction" class="fade">
<form data-bind="submit: $root.SaveTransaction">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>
Editing transaction</h3>
</div>
<div class="modal-body">
<label>Date</label> <input type="text" data-bind="text: Date"/></br>
<label>Category</label> <input type="text" data-bind="text: Category"/></br>
<label>Subcategory</label> <input type="text" data-bind="text: Subcategory"/></br>
<label>Amount</label> <input type="text" data-bind="text: Amount"/></br>
<label>Notes</label> <input type="text" data-bind="text: Notes"/></br>
<pre data-bind="text: ko.toJSON($root.EditingTransaction, null, 2)"></pre>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-primary" data-bind="click: $root.SaveTransaction">Save changes</a>
</div>
</form>
</div>
当数据分配给 editTransaction 时,对话框正在显示 - pre ko.toJSON 调试 HTML 显示如下:
{
"ID": "1231",
"TransactionType": "Withdrawal",
"Date": "2012-11-07",
"Category": "cat",
"Subcategory": "sub cat",
"Amount": "-50.00",
"currency": "GBP",
"Notes": "",
"AccountName": "Account 2",
"Payee": "Cheque"
}
我已经搜索并看到 cmets 在初始化模式时(加载页面时)数据不可用,但这对我来说没有意义(我相信点绑定是它们在查看数据何时更改以及调试语句工作正常)。我尝试在绑定代码中不使用 with 并且在数据绑定文本中使用对 $root 的引用。
所以,任何为什么它不起作用的想法都会很棒。
【问题讨论】:
-
您能否更具体地说明什么不适合您?是不是 rowClick 函数没有按预期工作?
-
是的,错过了 - 值没有显示表单。下面回答...
标签: twitter-bootstrap knockout.js custom-binding