我正在使用非常光滑的KnockoutJS库创建一个应用程序,但我遇到了麻烦。在html页面上,我有一个简单的<select>
控件,我想加载从Web服务返回的JSON数据。
我按如下方式定义了可观察数组:
var laborRow = function () {
this.positions = ko.observableArray([]);
};
当页面加载时,进行ajax调用并返回数据。在回调中,我执行以下操作:
success: function (msg) {
laborRow.positions = msg;
}
基于KO文档,我希望我能像这样设置结果:
laborRow.positions(msg);
然而,这只是抛出一个错误,指出“laborRow.positions in not a function”
html中的模板如下:
<tbody data-bind='template: {name: "laborRowTemplate", foreach: laborLine}'> </tbody>
</div>
<script type="text/html" id="laborRowTemplate">
<tr>
<td><select data-bind='options: positions, optionsText: "Title", optionsCaption: "select", value: selectedPosition '></select></td>
</tr>
</script>
laborRow
对象是ViewModel上绑定到页面的属性。无论出于何种原因,这都行不通。要添加另一个皱纹,如果我添加代码以查看observableArray并打印出一些数据,那么数据就在那里。所以它正在成功加载。
任何想法都会非常感激。
我的示例案例的完整代码:
var laborRow = function () {
this.positions = ko.observableArray([]);
};
var projectEstimate = function () {
this.laborLine = ko.observableArray([new laborRow()]);
};
var projectViewModel = new projectEstimate();
ko.applyBindings(projectViewModel);
//and the code in the callback function on ajax success
success: function (msg) {
laborRow.positions = msg;
//laborRow.positions(msg); **this does not work - error is laborRow.positions is not a function**
},
和html:
<tbody data-bind='template: {name: "laborRowTemplate", foreach:
laborLine}'> </tbody>
<script type="text/html" id="laborRowTemplate">
<tr>
<td><select data-bind='options: positions, optionsText:
"Title", optionsCaption: "select", value: selectedPosition '></
select></td>
</tr>
</script>
最后,感谢Sean在下面的评论,我能够通过修改回调中的代码来实现它,如下所示:
success: function (msg) {
projectViewModel.laborLine()[(projectViewModel.laborLine().length-1)].positionList(msg);
}
答案 0 :(得分:5)
问题是您实际上没有创建模型:
var laborRow = function () {
this.positions = ko.observableArray([]);
// will only be called if you call var some_var = new laborRow()
};
将您的功能更改为裸对象(如Knockout docs所示):
var laborRow = {
positions: ko.observableArray([])
};
您将能够致电laborRow.positions(msg);
并让它发挥作用。
修改强>
基于新代码,laborRow
仍未实例化 - 如果您在代码中的其他位置设置var laborRow
(也许在ajax请求周围),那么您需要确保您的调用堆栈如下所示:
projectViewModel.laborLine()[0].positions()
// This will return the array you're looking for.
// The key is that laborLine is a `getter` not an attribute
我曾经多次被“ko变量getters
而非attributes
”错误所困扰......你的代码可能会发生这种情况吗?