我想创建一种我已列出
的表格我将以某种模式将一些MVC模型映射到Ko ViewModel:
function User(data) {
this.userAlias = ko.observable(data.UserAlias);
this.userFirstName = ko.observable(data.UserFirstName);
this.userLastName = ko.observable(data.UserLastName);
};
function Day(data) {
var self = this;
self.date = ko.observable(data.Date);
self.dateStr = ko.observable(data.dateStr);
self.dtDay = ko.observable(data.dtDay);
self.value = ko.observable(data.Value);
self.users = ko.observableArray([]);
};
users
的{{1}}值是Day()
的数组
如何构建有效的数组以将其用作参数?
答案 0 :(得分:1)
如果您正在进行ajax调用,Day view model
看起来会像这样......
function Day(data) {
var self = this;
self.date = ko.observable(data.Date);
self.dateStr = ko.observable(data.dateStr);
self.dtDay = ko.observable(data.dtDay);
self.value = ko.observable(data.Value);
self.users = ko.observableArray([]);
var users = this.users;
//get users and push into observable array
$.getJSON('url', function (json) {
var mappedData = ko.utils.arrayMap(json, function (u) {
return new User(u.UserAlias, u.UserFirstName, u.UserLastName);
});
users(mappedData);
});
};
你的标记看起来像这样......
data-bind="foreach: users"
答案 1 :(得分:0)
您可以使用ko.toJSON,如下所示:
var raw = ko.toJSON(self.users());