我有以下代码:
self.usedTables = ko.computed(function () {
var usedTables = []; // doesn't need to be observable, just array
if (self.selectedJoins() != null) {
ko.utils.arrayForEach(self.selectedJoins(), function (j) {
if (j.firstTable() != '' && ko.utils.arrayIndexOf(usedTables, j.firstTable()) == -1) {
usedTables.push(j.firstTable());
}
if (j.secondTable() != '' && ko.utils.arrayIndexOf(usedTables, j.secondTable()) == -1) {
usedTables.push(j.secondTable());
}
});
}
return usedTables;
});
firstTable和secondTable都返回具有以下模式的对象:
var tableRecord = function () {
var self = this;
self.tableName = ko.observable();
self.fields = ko.observableArray([]);
};
在这个项目的其他地方,当我需要将ko.observableArray绑定到select时,我能够使用data-bind ="选项:allTables,optionsText:tableName&# 34 ;.即使数组为空,这也可以工作。但是,当我尝试以类似的方式绑定上述代码的结果时,如果数组返回一个空集,我会收到错误:
" JavaScript运行时错误:无法获取属性' tableName'未定义或空引用"。
如何将其绑定到选择?
根据请求,添加加载allTables的代码
self.seedTables = function () {
var regis = new tableRecord();
regis.tableName('regis');
regis.fields.push(new fieldClass('internal_id', 'string'));
regis.fields.push(new fieldClass('exam', 'string'));
regis.fields.push(new fieldClass('admin_date', 'date'));
regis.fields.push(new fieldClass('pass_fail', 'string'));
self.allTables.push(regis);
var ei = new tableRecord();
ei.tableName('exam_info');
ei.fields.push(new fieldClass('exam', 'string'));
ei.fields.push(new fieldClass('name', 'string'));
ei.fields.push(new fieldClass('exam_type', 'string'));
ei.fields.push(new fieldClass('type', 'string'));
ei.fields.push(new fieldClass('admin_date', 'date'));
self.allTables.push(ei);
...