【发布时间】:2013-02-03 13:15:11
【问题描述】:
不确定这里出了什么问题,但 KnockoutJS 在查找我的 MasterViewModel 中的可观察数组时遇到了一些问题。使用 2.2.1 和 jQuery 1.8.x 以及不是我的第一个 KJS 应用程序。这里是:
初始化
$(function() {
window.vm = new MasterViewModel();
ko.applyBindings(vm);
});
视图模型
function MasterViewModel(data) {
var self = this;
self.currentAppView = ko.observable();
// Users
self.userList = ko.observableArray([]);
self.templateListGetter = ko.computed(function() {
$.getJSON("/user/list"), function(data) {
var mapped = $.map(data, function(item) { return new userModel(item) });
self.userList(mapped);
};
});
self.goToAppView = function(appView) {
location.hash = '!/' + appView;
};
Sammy(function() {
this.get('#!/:appView', function() {
self.currentAppView(this.params.appView);
$('.appview').hide();
ko.applyBindings(new window[this.params.appView+'VM']());
});
this.notFound = function(){
location.hash = "!/dashboard";
}
//this.raise_errors = true;
}).run();
}
观点
<table class="table table-bordered table-striped">
<tbody data-bind="foreach: userList">
<tr>
<td data-bind="text: guid"></td>
<td data-bind="text: firstName"></td>
<td data-bind="text: lastName"></td>
<td data-bind="text: email"></td>
<td data-bind="text: updated"></td>
<td data-bind="text: suspended"></td>
</tr>
</tbody>
</table>
我正在加载一个简单的表格
即使在仔细检查了几件事(例如将defer="defer" 添加到我的 JS 标记并确保 userList 存在之后,它也无法找到 observableArray。它给出了错误:
Message: ReferenceError: userList is not defined;
Bindings value: foreach: userList Error {}
有人知道发生了什么吗?
更新
对于那些想知道每次哈希更改时调用什么的人:
function usersVM() {
// Data
var self = this;
// Behaviours
$('#users').show();
}
【问题讨论】:
标签: javascript mvvm knockout.js knockout-2.0